override func viewDidLoad() {
super.viewDidLoad()
layout()
}
private func layout() {
view.addSubview(superViewBase)
superViewBase.snp.makeConstraints {
$0.deges.equalToSuperview().offset(20)
//...
}
}
keyWindow
의 rootViewController라서 view.superview = nilsafeAreaLayourGuide
로 잡아주면 안전하다..isActive = true
해야 적용된다.NSLayoutConstraint
에서 item
(constraint를 주고 싶은 컴포넌트), toItem
(기준이 될 컴포넌트), attribute
parameter를 통해 같은 효과를 낼 수 있다.class MainViewController: UIViewController {
private lazy var superViewBase: UIView = {
var view = UIView()
view.backgroundColor = .gray
return view
}()
private lazy var subViewBase: UIView = {
var view = UIView()
view.backgroundColor = .blue
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
layout()
}
private func layout() {
view.addSubview(superViewBase)
superViewBase.translatesAutoresizingMaskIntoConstraints = false
superViewBase.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
superViewBase.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
NSLayoutConstraint(item: superViewBase,
attribute: .top,
relatedBy: .equal,
toItem: view.safeAreaLayoutGuide,
attribute: .top,
multiplier: 1.0,
constant: 16).isActive = true
/*
subViewBase.snp.makeConstraints {
$0.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(16)
}
*/
superViewBase.heightAnchor.constraint(equalToConstant: 400).isActive = true
//...
}
참고자료
https://developer.apple.com/documentation/uikit/nslayoutanchor/1500937-constraint