Krodak
Krodak

Reputation: 1493

Set UIToolbarPosition for created UIToolbar

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.

Upvotes: 3

Views: 2403

Answers (1)

Dustin
Dustin

Reputation: 6803

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).

Upvotes: 2

Related Questions