iOS 11 Swift-导航&状态栏&tableview适配
来源:互联网 发布:博雅软件股份有限公司 编辑:程序博客网 时间:2024/06/01 07:29
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; }
搞定了~
阅读全文
0 0
- iOS 11 Swift-导航&状态栏&tableview适配
- iOS TableView 使用(swift)
- ios swift tableview
- 奔五的人学iOS:swift对 状态栏、导航条若干技巧
- IOS swift 自定义Tableview cell
- iOS导航栏和状态栏
- ios 导航栏状态栏设置
- 关于iOS 11 tableView跟导航栏之间有空白和tableview的头视图问题
- ios 11 tableView穿透导航栏 同时让section悬浮在导航栏下面
- iOS 导航和tableView/collectionView影响界面
- iOS 导航和tableView/collectionView影响界面
- ios TableView那些事2(Swift 一) 初见TableView
- ios TableView那些事2(Swift 二) 初见TableView Grouped
- 【IOS】Swift编写可折叠的tableView
- iOS swift 自定义tableView不显示cell
- swift中tableView导航栏渐变和下拉变大效果
- iOS设置状态栏和导航栏颜色
- iOS 7, 导航栏和状态栏自定义
- java.net.SocketException: Software caused connection abort
- linux进程通信
- 【ES】ElasticSearch入门
- 对谷歌搜索列表的剖析
- Windows Socket的使用 总结
- iOS 11 Swift-导航&状态栏&tableview适配
- JavaScript Array map()方法
- css设置多余显示省略号
- 未能加载文件或程序集“SuperMap.Mapping, Version=7.0.0....的解决方法
- poj 1308 is it a tree?(图的性质||基础并查集) (同hdu 1272 小希的迷宫 )
- DFS-lintcode恢复ip地址(Restore IP Addresses)
- 【Android】侧滑菜单栏功能的实现(类似于QQ)
- Java 枚举(enum) 详解7种常见的用法
- 系统还原软件原理