// UIViewExtensions.swift
import UIKit
import SnapKit
public struct SafeArea {
let view: UIView
var top: ConstraintItem {
if #available(iOS 11, *) {
return view.safeAreaLayoutGuide.snp.top
} else {
return view.viewController!.topLayoutGuide.snp.top
}
}
var left: ConstraintItem {
return view.snp.left
}
var bottom: ConstraintItem {
if #available(iOS 11, *) {
return view.safeAreaLayoutGuide.snp.bottom
} else {
return view.viewController!.bottomLayoutGuide.snp.bottom
}
}
var right: ConstraintItem {
return view.snp.right
}
}
//MARK:- 安全布局
extension UIView {
public var safeArea: SafeArea {
return SafeArea(view: self)
}
}
使用
[headerView, segmentedView, scrollTableView].forEach { view.addSubview($0) }
headerView.snp.makeConstraints { (make) in
make.top.equalTo(self.view.safeArea.top)
make.left.width.equalTo(self.view)
make.height.equalTo(headerView.height)
}