UINavigationController barstyle property changes layout

戏子无情 提交于 2019-12-24 12:05:16

问题


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?


回答1:


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;


来源:https://stackoverflow.com/questions/7287672/uinavigationcontroller-barstyle-property-changes-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!