美文网首页
Swift 4.0 问题小结

Swift 4.0 问题小结

作者: Superman168 | 来源:发表于2018-03-27 14:09 被阅读0次

最近跟着刀哥用 Swift 写微博,顺便学习了一下 Swift 4.0 ,遇到了一些问题,特此小结,总结一下,供跟我一样遇到类似问题的小伙伴以参考:

1. 自定义导航栏的问题:

先看图:


IMG_FE52FACA6E3F-1.jpeg

在 IOS 11.2.6 上运行,Swift 4.0.看到不少人都遇到了这个问题,在 IOS 11 以后出现的问题:
分析一下:
从视图层次结构图上可以看出,其实 NavigationBar (上图)的高度并没有改变依然还是 熟悉的 64(IPhone X 的88除外),只是NavigationBar 的 _UINavigationBarContentView(下图) 高度上移了 20px,到了故出现这种情况:

NavigationBar.png UINavigationBarContentView.png

找到问题就好办了,只要把 UINavigationBarContentView 的 frame 设置向下 20px 即可。

解决办法:

继承于系统的 UINavigationBar,重写 layoutSubviews 方法,遍历 subViews 设置 frame 区域即可。代码如下:

import UIKit

class QCustomNavBar: UINavigationBar {

    override func layoutSubviews() {
        super.layoutSubviews()
        
        for subView in self.subviews {
            let stringFromClass = NSStringFromClass(subView.classForCoder)
            print("-----\(stringFromClass)")
            
            // "Barbackground" 为大写B搞成小写b了,一字之差
            if stringFromClass.contains("BarBackground"){
                subView.frame = self.bounds
            }else if (stringFromClass.contains("UINavigationBarContentView")){
                subView.frame = CGRect(x: 0, y: 20, width: UIScreen.cz_screenWidth(), height: 44)
            }
        }
    }

}

使用的时候直接使用:

    /// 自定义导航栏
    lazy var navigationBar = QCustomNavBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
后话

本来很简单的一件事,谁想期间也遇到了一些波折,看到上面的注释了 ‘’ BarBackground” 敲成“ Barbackground”了,一字之差,半天没效果,颇费时间调试,还是 copy 牢靠。
可以参考stackoverflow上大神的
https://stackoverflow.com/questions/45711765/how-to-correctly-set-uinavigationbar-height-in-ios-11

2. 应用无响应的问题。

原代码

       window = UIWindow()
       window?.backgroundColor = UIColor.white
       window?.rootViewController = QCMainViewController()
       window?.makeKeyAndVisible()

在 IOS 8.3 上运行,你会发现界面无响应,控制台报错:
unexpected nil window in _UIApplicationHandleEventFromQueueEvent, _windowServerHitTestWindow: <UIWindow: 0x7fef1e727a10; frame = (0 0; 0 0); gestureRecognizers = <NSArray: 0x7fef1e703b80>; layer = <UIWindowLayer: 0x7fef1e727430>>

解决方法:

        // 这样初始化,设置 frame,防止 8.3 低版本点击无响应。报错
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = UIColor.white
        window?.rootViewController = QCMainViewController()
        window?.makeKeyAndVisible()

再运行就 OK 了。

3. 设置 TableView 内容缩进

 tableView?.contentInset = UIEdgeInsets(top: navigationBar.bounds.height-20, left: 0, bottom: 0, right: 0)

经测试在 IOS 8.3 和 11 正常显示。
未设置页面的自动缩进 automaticallyAdjustsScrollViewInsets = false 和 底部 tabBar 的缩进

tableView?.contentInset = UIEdgeInsets(top: navigationBar.bounds.height-20, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)

设置反而会有问题,底部空出了 tabbar 高度的一片空白。

4.GCD 的API 改动

GCD 在 Swift 4.0 中改动还是比较大的,一时还得适应一下:

随便列举一下,比如 GCD 中的 dispatch after 延迟执行

Swift 3.0 中的写法:

DispatchQueue.main.after(when: now()) {
           <#code#>
}

swif 4.0 写法如下:

let time = DispatchTime(uptimeNanoseconds: UInt64(1.0))
DispatchQueue.main.asyncAfter(deadline: time) {
}

相关文章

网友评论

      本文标题:Swift 4.0 问题小结

      本文链接:https://www.haomeiwen.com/subject/senlcftx.html