iOS 11运行项目变成了这个样。。
总结几个点:
1.状态栏重叠,字体颜色需要白色不是黑色。
2.导航栏上移了20个点。
3.tableview 上下都有空白。
开工,
一、首先解决状态栏问题。
1.infoplist中设置View controller-based status bar appearance 值为NO。
2.在appdelegate中加入UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent;将字体设为白色(没有第一步,第二部没效果)。
OK了
二、导航栏错位问题
我的项目是自定义NavigationBar,会出现这个问题。使用系统的NavigationBar不会有这个问题。需要重写下NavigationBar中的layoutSubviews方法。
先上OC的代码,百度这个问题的时候各位前辈给出的答案。
for (UIView *view in self.subviews) {
if([NSStringFromClass([view class]) containsString:@"Background"]) {
view.frame = self.bounds;
}
else if (NSStringFromClass(view) containsString:@"ContentView"]) {
let frame = view.frame;
frame.origin.y = statusBarHeight;
frame.size.height = self.bounds.size.height - frame.origin.y;
view.frame = frame;
}
原理是
1.遍历NavigationBar所有的子视图
2.通过类名找出下面这两个东西,具体可以看看项目的层级关系
<_UIBarBackground: 0x7ff38043a320; frame = (0 0; 320 44); userInteractionEnabled = NO; layer = <CALayer: 0x60000023ac80>>
<_UINavigationBarContentView: 0x7ff38043add0; frame = (0 0; 320 44); layer = <CALayer: 0x60000023ad80>>
3.更改相应的frame
Swift代码
override func layoutSubviews() {
super.layoutSubviews();
//注意导航栏及状态栏高度适配
frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
for view in subviews {
let viewbg = subviews[0];//_UIBarBackground
let viewcon = subviews[2];//_UINavigationBarContentView
print(view);
if(view == viewbg){
view.frame = bounds;
}else if(view == viewcon){
var frame = view.frame;
frame.origin.y = 20;
frame.size.height = self.bounds.size.height - frame.origin.y;
view.frame = frame;
}
}
}
注:Swift中试了很久没找到把对象的类名以字符串表示的方法,所以就做不了比对。只能把子视图调出来进行比对。请大牛指教。。
这样导航和状态栏就OK了
三、tableview
1.automaticallyAdjustsScrollViewInsets = false;被废弃了
VC代码中写下面三句,为了兼容11以下的版本需要加上automaticallyAdjustsScrollViewInsets。
extendedLayoutIncludesOpaqueBars = true;
edgesForExtendedLayout = .top;
automaticallyAdjustsScrollViewInsets = false;
2.tableview中加入下面代码
if #available(iOS 11.0, *) {
tableview?.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.never
} else {
// Fallback on earlier versions
};
3.iOS 11中如果不实现-tableView: viewForFooterInSection: 和 -tableView: viewForHeaderInSection:,那么-tableView: heightForHeaderInSection:和- tableView: heightForFooterInSection:不会被调用。需要增加下面方法。
// tableview 底部视图
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil;
}
// tableview 头部视图
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return nil;
}
搞定了~