iOS13 适配指南(持续更新中)
2019-10-11 本文已影响0人
枫子jun
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
适配
设置modalPresentationStyle为fullScreen
vc.modalPresentationStyle = .fullScreen
遇到的设备旋转问题
AppDelegate.swift类中通过supportedInterfaceOrientationsFor方法改变设备方向时
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if <#需要横屏#> {
return .landscapeRight
}else{
return .portrait
}
}
需要设置modalPresentationStyle为fullScreen才有效果