Why is Apple doing this in their example?

Discussion in 'iOS Programming' started by Compile 'em all, May 18, 2010.

  1. Compile 'em all macrumors 601

    Compile 'em all

    Apr 6, 2005
    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).

    	// 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,
    	[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!
  2. idelovski macrumors regular

    Sep 11, 2008
    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.

    [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*/,
    [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.

Share This Page