Duck
Duck

Reputation: 35973

Iphone - UIToolbar automatic position

I have an application where a UIToolBar is to be constantly on the bottom side of the screen. No matter the device orientation.

The toolbar must have the same screen width and always be justified to the bottom side.

If the device rotates, the toolbar has to assume the new width and continue to be justified on the bottom.

Is there a way to do that programmatically?

thanks for any help.

Upvotes: 3

Views: 7140

Answers (3)

Andrew Johnson
Andrew Johnson

Reputation: 13286

First off, auto-rotation can be a little tricky. For example, if you are using a UITabBarController, you will need to subclass it and add the appropriate delegate methods. You should read up on auto-rotation via the Apple docs and Google, before really diving in.

To answer your question specifically though, here is how you would need to declare the UIToolbar such that it will auto-rotate when you have the app set up to do so:

// I keep this definition in a file called constants.h since I use it a lot
#define SCREEN_FRAME [[UIScreen mainScreen] applicationFrame]

UIToolbar *tb = [[[UIToolbar alloc]init]autorelease];
// this frame will position a toolbar at the bottom of the screen
tb.frame = CGRectMake(0,
            SCREEN_FRAME.size.height-tb.frame.size.height,
            SCREEN_FRAME.size.width, 
            tb.frame.size.height);
//Setting the auto-resizing mask will make the toolbar resize when the viewController 
//it resides in rotates.
tb.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

Upvotes: 6

kennytm
kennytm

Reputation: 523444

Use a navigation controller, and make use of the toolbarItems property.

Upvotes: 0

Ole Begemann
Ole Begemann

Reputation: 135558

Yes. Have a look at autoresizing masks.

Upvotes: 0

Related Questions