1

I have a modal view controller which displays a navigation controller. The navigation controller in-turn has a regular UIViewController as its root view controller. The only UI element that the above-mentioned UIViewController has is a UISwitch.

Now here's the problem: When I change the barStyle property of the navigation controller, the layout of the UISwitch inside the UIViewController changes. Here's what I am talking about:

If I don't set the barStyle property, here's what I get:

http://img535.imageshack.us/img535/2281/plaini.png

The UISwitch is now in its 'exepected' place.

Now if I set the barStyle property,

navController.navigationBar.barStyle = UIBarStyleBlackTranslucent;

Notice that the UISwitch is behind the navigation bar:

http://img853.imageshack.us/img853/2377/blackya.png

Here's the code for the UISwitch in the UIViewController:

- (void)viewDidLoad
{
    UISwitch* mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
    [self.view addSubview:mySwitch];
    [mySwitch release];
}

Can someone help me understand what's happening?

Ravi
  • 7,929
  • 6
  • 38
  • 48

1 Answers1

3

The Frame shifts when you use the UIBarStyleBlackTranslucent (which is actually deprecated) property because it assumes you want your view to be underneath

The Apple Documentation says to use the following since UIBarStyleBlackTranslucent is deprecated:

navController.navigationBar.barStyle = UIBarStyleBlack;
navController.navigationBar.translucent = YES;

You could try shifting your view back in the correct place or try using the following:

navController.navigationBar.tintColor = [UIColor blackColor];
navController.navigationBar.translucent = YES;
Suhail Patel
  • 13,644
  • 2
  • 44
  • 49
  • 1
    I didn't think barStyle would have that much of an effect on the view's appearance. Thanks a lot!! It's surprising Xcode does not show UIBarStyleBlackTranslucent as a deprecated option! – Ravi Sep 02 '11 at 20:19
  • Also, I had to shift my UI elements downwards despite using the above two lines of code your provided. Looks like ".translucent" property decides whether the frame behind it needs to be shifted, cos when I set the property to NO, I didn't have to shift anymore. – Ravi Sep 02 '11 at 20:33
  • Not sure how you have laid out your code but couldn't you just shift the view that you are adding the button to 44 pixels down? – Suhail Patel Sep 02 '11 at 20:38