Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Compile 'em all

macrumors 601
Original poster
Apr 6, 2005
4,131
359
In Apple's UICatalog example, there is a snippet that they wrote to position a toolbar at the bottom of the screen in a navigation controller view (in the ToolbarViewController.m).

Code:
	// size up the toolbar and set its frame
	[toolbar sizeToFit];
	CGFloat toolbarHeight = [toolbar frame].size.height;
	CGRect mainViewBounds = self.view.bounds;
	[toolbar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
								 CGRectGetMinY(mainViewBounds) + CGRectGetHeight(mainViewBounds) - (toolbarHeight * 2.0) + 2.0,
								 CGRectGetWidth(mainViewBounds),
								 toolbarHeight)];
	
	[self.view addSubview:toolbar];

What I don't understand is why are they shifting the toolbar downwards by 2px (the +2.0 part)? This effectively makes the toolbar look 2px shorter than a normal toolbar. Is this intentional or is there something I am missing here?

Thanks guys!
 
Well, I was even more curious why are they subtracting toolbarHeight TWICE.

I have changed the code to this and it produces the same result as the original (if you uncomment that "+2" part). And you are right.

Code:
[toolbar sizeToFit];
CGFloat toolbarHeight = [toolbar frame].size.height;
CGRect  mainViewBounds = self.view.bounds;
CGRect  navRect = self.navigationController.isNavigationBarHidden ? CGRectZero : self.navigationController.navigationBar.bounds;
CGRect  toolRect = CGRectMake(CGRectGetMinX(mainViewBounds),
                              CGRectGetMinY(mainViewBounds) + CGRectGetHeight(mainViewBounds) - (toolbarHeight + navRect.size.height) /*+ 2.0*/,
                              CGRectGetWidth(mainViewBounds),
                              toolbarHeight);
[toolbar setFrame:toolRect];

Toolbar ends up with height of 44 at y=372. Its parent view is at y=64, status bar plus navigation bar, that is 20 + 44. So, 372+64 = 436. Toolbar would start at y=436 and with its height of 44 it would be perfectly placed at the bottom. 436 + 44 = 480. But, they are placing it 2 pixels below that position, at 438 to 482.

It probably looks cooler if it's thinner.

In the end, it is actualy strange they are not reducing main view for the height of navigation bar. I think that the proper way to code all of this would be to decrease self.view.frame by the height of navigation bar.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.