iOS13 适配指南(持续更新中)

1. UINavigationBar 设置按钮边距崩溃

iOS11开始 UINavigationBar 使用自动布局,左右两边会有16或20的边距。
为了美观,通常的做法:定义UINavigationBar子类,重写layoutSubviews方法,遍历subviews获取_UINavigationBarContentView,然后设置layoutMargins属性

let space = kDefultFixSpace //距离边缘8pt
for view in subviews {
    if NSStringFromClass(view.classForCoder).contains("ContentView") {
       //ios13 禁止调用私有方法
       view.layoutMargins = UIEdgeInsetsMake(0, space, 0, space)
    }
}

这种方式在ios13会Crash

解决方法

使用设置frame的方式

let space = kDefultFixSpace //距离边缘8pt
for view in subviews {
    if NSStringFromClass(view.classForCoder).contains("ContentView") {
        if #available(iOS 13.0, *) {
            let margins = view.layoutMargins
            view.frame = CGRect(x: -margins.left + space, y: margins.top, width: margins.left + margins.right - space*2 + view.frame.size.width, height: margins.top + margins.bottom + view.frame.size.height)
        } else {
            view.layoutMargins = UIEdgeInsetsMake(0, space, 0, space)
        }
    }
}

2. 模态弹出默认样式改变 UIModalPresentationStyle

在ios13下,使用present模态加载方式,叠压效果如下:

ios13 present 默认效果

主要是因为UIModalPresentationStyle增加了automatic新的类型,ios13默认为automatic,之前版本默认是fullScreen

适配

设置modalPresentationStylefullScreen

vc.modalPresentationStyle = .fullScreen

遇到的设备旋转问题

AppDelegate.swift类中通过supportedInterfaceOrientationsFor方法改变设备方向时

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if <#需要横屏#> {
        return .landscapeRight
    }else{
        return .portrait
    }
}

需要设置modalPresentationStylefullScreen才有效果

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。