iOS11 SafeArea 对应swift 片段
2018-05-05 本文已影响455人
小杨哥
在开发过程中遇到tableView内容下移20pt或下移64pt的问题,对当tableView超出安全区域时系统自动调整了SafeAreaInsets
值,进而影响adjustedContentInset
值,在iOS 11中决定tableView的内容与边缘距离的是adjustedContentInset
属性,而不是contentInset
。
adjustedContentInset = safeAreaInset + contentInset
在 UIViewContoller 实现 viewSafeAreaInsetsDidChange
方法
override func viewSafeAreaInsetsDidChange() {
if #available(iOS 11.0, *) {
super.viewSafeAreaInsetsDidChange()
NSLog("viewSafeAreaInsetsDidChange-%@",NSStringFromUIEdgeInsets(self.view.safeAreaInsets))
} else {
// Fallback on earlier versions
}
self.updateOrientation()
}
对应的 updateOrientation() 方法内容为
/**
更新屏幕safearea frame
*/
func updateOrientation()
{
if #available(iOS 11.0, *) {
var frame = self.customView.frame
frame.origin.x = self.view.safeAreaInsets.left
frame.size.width = self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right
frame.size.height = self.view.frame.size.height - self.view.safeAreaInsets.bottom
self.customView.frame = frame
}
}