I'm writing app which is aimed only for iOS5 devices, so I'm trying to maximize usage of new appearance API.
I can change background of my UIToolbar with following method:
[[UIToolbar appearance] setBackgroundImage:<myImage> forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
Everything works fine, but I'm looking for more customization, so I was wondering about usage of UIToolbarPosition and I run into some problems. Using InterfaceBuilder or adding UIToolbar programmatically I'm positioning it at the top of main view - so I'm expecting UIToolbarPosition to be set as UIToolbarPositionTop, but from what I'm testing it doesn't work automaticly nor can I find any API which allows me to set UIToolbarPosition (Yes, I googled it already).
So - main question - how to create UIToolbar and tag it properly, so it can response only to UIToolbarPositionTop or UIToolbarPositionBottom, so I can use:
[[UIToolbar appearance] setBackgroundImage:<myImage_1> forToolbarPosition:UIToolbarPositionTop barMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:<myImage_2> forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];
EDIT: More details, as asked.
I have multiply toolbars in my application - for instance, toolbars at top of the screen on 2 screens, toolbars acting as accessory views for keyboard and 2 toolbars at the bottom of modals screens. I'd like to maximize usage of new appearance API instead of customizing toolbars on each screens, hence I'm asking about whole UIToolbarPosition thing and how to use it.
I know I can achieve what I want just by customizing each UIToolbar separately, but I'm just curious about new API and UIToolbarPosition usage.
UIToolbarPostion isn't a property that you're supposed to be setting programmatically - instead, it allows you to tell the program how to handle a toolbar when it's in different positions. This is mostly for toolbars that are getting pushed around by screen changes (autorotation) or are on something like a navigation controller that has variable content.
That being said, if you want to directly access your toolbars so that you can use them/set properties/etc there are a couple of methods. It sounds like you know what tagging is, and this is a valid method - just give the toolbar a tag in IB or programmatically (either edit the tag property in the side bar for IB or use the .tag property when you declare the toolbar). Then you can use the viewWithTag method to access your toolbar. However, a better method would be to just create an IB property for the toolbar (same as with labels or buttons) by control-dragging over to the header file from the toolbar. Then you could just write [nameOfToolbarProperty doSomeMethod]. If you're creating your toolbar with code then just make a reference to it the same way e.g.
UIToolbar *tref = [/*toolbar creation code*/];
In conclusion, your code
[[UIToolbar appearance] setBackgroundImage:<myImage_1> forToolbarPosition:UIToolbarPositionTop barMetrics:UIBarMetricsDefault];
could be made to work by adding
//Connect this to your toolbar in Interface Builder
#property (weak, nonatomic) IBOutlet UIToolbar *tref;
to your header. Then just do
[tref setBackgroundImage:<myImage_1> forToolbarPosition:UIToolbarPositionTop barMetrics:UIBarMetricsDefault];
Note that this just tells the program what image to display if the toolbar is in the top position - it does not set the position of the toolbar. UIToolbarPosition is a constant (so you cannot set it).
Related
How can I provide a visually distinct bar button item background image for bar buttons with the UIBarButtonItemStyleDone style and the editing state of the Edit/Done button? None of the documented UIControlState values in the UIBarButtonItem appearance proxy's setBackgroundImage:forState:barMetrics: seem to do the trick.
I'm not sure why this was never answered but if you are still looking then simply create a property for a BarButtonItem in your .h, assign it in IB, then set the background property of that barButtonItem. Works fine for me and never "changes back to default appearance".
- (void)viewDidLoad
{
[super viewDidLoad];
[self.barButton setBackgroundImage:[UIImage imageNamed:#"YOURIMAGE"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
I have just found and posted a possible solution to this problem.
You can 'inject' custom styles using a UIBarButtonItem category:
Customizing UIBarButtonItem "Done" style and "Plain" style separately using UIAppearance
I want to create a custom UITabBar and am familiar with the appearance proxy. I know I can set its background image to whatever I want. How can I change the selected state of each tab? Essentially I would like to remove the gloss/shine effect.
You need to grab the actual UITabBarItem
Something like this should do the trick for individual items
UITabBar *tabBar = tabBarViewController.tabBar;
for(UITabBarItem *tabItem in tabBar.items)
{
//in reality you will probably change these images and grab from the array individually
UIImage *selectedImage = [UIImage imageNamed:#"selected.png"];
UIImage *unselectedImage = [UIImage imageNamed:#"unselected.png"];
[tabItem setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
}
alternatively you can just use the proxy to alter all UITabBarItems using
[UITabBarItem appearance]
You may use the appearance proxies for UIBarItem and UITabBarItem to accomplish this.
From Apple's documentation:
Customizing Appearance
In iOS v5.0 and later, you can customize the
appearance of tab bars by setting item label text attributes using
appearance selectors declared by UIBarItem. You can also use the
methods listed in “Customizing Appearance.” You can customize the
appearance of all segmented controls using the appearance proxy (for
example, [UITabBarItem appearance]), or just of a single tab bar. You
can also provide finished selected and unselected images using the
methods listed in “Managing the Finished Selected Image”; these
methods, though, so not participate in the UIAppearance proxy API (see
UIAppearance). UIKit does now provide any automatic treatment to
finished images. For good results, you must provide finished selected
and unselected images in matching pairs using
setFinishedSelectedImage:withFinishedUnselectedImage:.
How to add image to lower bar in tab based application
I can get the
self.navigationitem
but how to access the lower toolbar anda add background image to it
best regards
What you're looking for is a UITabBarITem that you can obtain from the items property of a UITabBar, or more easily from the tabBarItem property of your UIViewController.
Then I guess you can set your image using image property...
In conclusion :
self.tabBarItem.image = [UIImage imageNamed:#"myBeautifulImage.png"];
might be what you're looking for (self being, ofc, your UIViewController)...
EDIT: but if your "lower bar in tab based application" is a UIToolbar (/me :grins:..) then what you are looking for is a standard UIBarButtonItem that can be obtained from UIToolbar.items property OR some IBOutlet property if you wired it using Interface Builder.
In any case, I guess looking at Apple sample codes might be a good idea.
I have a UITableViewController where I added a "editButtonItem" in the navigation bar :
self.navigationItem.leftBarButtonItem = self.editButtonItem;
No magic here, but I try to define the color (background and foreground/textcolor) of this button.
I read in the Apple forum somewhere that the button changes the color if I change the navigationbar to the same color, well despite the fact that I do not get this to work either (for testing) I do not want to touch the navigationbr itself, just the button.
Since this button is already predefined I am not sure how to handle this. Do I need to overwrite the button with my own definition or can I just simply apply a new style (if so how ?)
Thx
If everything fails, you can tint the bar in the viewDidLoad of your childViewController
[self.navigationController.navigationBar setTintColor:[UIColor colorWithHue:50 saturation:50 brightness:.2 alpha:.5]];
The only way I can find to make a UIBarButtonItem with a different color is to use a custom view and the initWithCustomView: method. Predefined buttons and those created using initWithTitle:target:action: cannot change their background color.
Take a look at this SO question: UIBarButtonItem with color?
These links might also help:
http://www.iphonedevsdk.com/...
http://www.insanelymac.com/...
I am using a TTPhotoViewController subclass from the Three20 library for showing images from a web location. The images load up fine but the navigation bar and toolbar show up with the default tint. I am using a nav bar with a custom tint (set in the MainWindow.xib)
I tried these things to get it to show black translucent bars but none of these seem to work.
setting the navigation bar style to black translucent in MainWindow.xib
setting the navigation bar style to black opaque in MainWindow.xib
Explicitly setting the navigationbar style to black translucent in the subclass's viewWillAppear:
Can someone please tell me why this would happen and how I can solve this? Thanks.
EDIT: I'm an idiot. You're trying to set the style, not the color. The below is all valid information, but what you almost certainly want is the navigationBarStyle property defined by TTViewController. Sorry.
Three20 has a "style sheet" mechanism built into it, the intended purpose of which is to save you from having to set tint colors, fonts, etc. on all of the many UI objects in your app, over and over. However, if you don't know it's there, you end up in exactly this situation. What you need to do is:
Create a subclass of TTDefaultStyleSheet in your application, and override at least this method:
- (UIColor*)navigationBarTintColor {
return RGBCOLOR(119, 140, 168);
}
Someplace in your app (probably applicationDidFinishLaunching:), call:
[TTStyleSheet setGlobalStyleSheet:
[[[YourStyleSheetClass alloc] init] autorelease]];
(You might want to browse around in TTDefaultStyleSheet.h, because there are a whole pile of other styles defined there that are used by the framework, and that you might also want to override.)