Pheepster
Pheepster

Reputation: 6347

Strange behavior with UIToolbar in iOS app

In my current project I have an app based on a navigation controller. Some of the view in the app require a toolbar at the bottom of the view. I am creating the toolbar programmatically with the following (excerpted from viewDidLoad in a table view controller):

UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshList)];

[reloadButton setTitle:@"Refresh"];
[reloadButton setTintColor:[UIColor whiteColor]];

self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationController.toolbar sizeToFit];
CGFloat toolbarHeight = [self.navigationController.toolbar frame].size.height;
[self.navigationController.toolbar setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
                                                       438,
                                                       CGRectGetWidth(self.view.bounds),
                                                       toolbarHeight)];
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:reloadButton, nil];
[self setToolbarItems:toolbarItems];
[self.navigationController setToolbarHidden:NO];

This has worked well in most views. However, I am now incorporating it into another view using identical code. In that controller, the toolbar appears about an inch above the bottom of the view--it doesn't snap to the bottom of the view as it does in other view controllers. Once I have navigated to the problem view, the other toolbars in other view begin exhibiting the same behavior. This is only happening in the simulator, not on my physical device. However, I currently have only an iPhone 4 as my physical device. Is this a bug in the simulator or an indication of an issue somewhere? THanks!

Upvotes: 0

Views: 217

Answers (1)

Robert Wagstaff
Robert Wagstaff

Reputation: 2674

You are setting the y position to a hardcoded value of 438. This will result in it not appearing at the bottom of the screen for the taller iPhone 5's). You should calculate the y position as well perhaps with:

CGRectGetMaxY(self.view.bounds) - toolbarHeight

this would result in:

[self.navigationController.toolbar setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
                                                   CGRectGetMaxY(self.view.bounds) - toolbarHeight,
                                                   CGRectGetWidth(self.view.bounds),
                                                   toolbarHeight)];

It could also have to do with the presence of a navigation bar and the status bar

Upvotes: 1

Related Questions