iOS11解决代码添加UINavigationBar导航栏显示异
问题描述:
由于某些需求,项目中的导航栏用的都是自定义的方式添加的,而把uinavigationController默认的navigationbar隐藏调了。在iOS10之前,下面的那串代码都能正常显示,代码如下:
CGRect frame =CGRectMake(0,0,CGRectGetWidth(self.view.frame),64);
_customNavigationBar= [[UINavigationBaralloc]initWithFrame:frame];
_customNavigationBar.barTintColor=G_Theme_NavBar_Color
_customNavigationBar.translucent=NO;
[self.view addSubView:_customNavigationBar];
但是在iOS11(xcode9.0-beta4)中:就会出现导航栏内容靠上的问题,如图:
查了一下iOS11最新的sdk,没找到可以调整这个的方法。果断联系apple的开发团队,得到了以下的反馈内容:
THe layout is incorrect. The navigation bar height should be 44pts tall (not 64) and the frame.origin.y should be placed at topLayoutGuide.height. Your navigation bar delegate should then implement -positionForBar: to return UIBarPositionTopAttached.
You can see how UINavigationController places the navigation bar as an example, but it is almost certainly simpler to just use a UINavigationController to get the navigation bar placed correctly instead.
最终解决代码:
CGRect frame =CGRectMake(0,20,CGRectGetWidth(self.view.frame),44);//目前该项目中的所有界面都会显示状态栏,所以此处直接先死frame
_customNavigationBar= [[UINavigationBaralloc]initWithFrame:frame];
_customNavigationBar.barTintColor=G_Theme_NavBar_Color;
_customNavigationBar.translucent=NO;
_customNavigationBar.delegate=self;
- (UIBarPosition)positionForBar:(id)bar {
returnUIBarPositionTopAttached;
}