iOS 11 Swift-导航&状态栏&tableview适配

在iOS 11中遇到了状态栏重叠、导航栏错位和TableView空白问题。解决方案包括在Info.plist设置status bar appearance为NO,并在AppDelegate中设置statusBarStyle为白色。对于导航栏错位,需要重写NavigationBar的layoutSubviews方法。对于TableView,由于automaticallyAdjustsScrollViewInsets被废弃,需在代码中进行手动调整以兼容不同版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    }

搞定了~





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值