반응형
아래 코드 참고.
import UIKit
import SnapKit
import Foundation
class MyMainView: UIViewController {
let safetyArea: UIView = {
let v = UIView()
return v
}()
lazy var myButtonTop: UIButton = {
let btn = UIButton()
btn.setTitle("My Button(T)", for: .normal)
btn.backgroundColor = UIColor.blue
return btn
}()
override func viewDidLoad() {
super.viewDidLoad()
setBaseView()
setView()
}
func setBaseView(){
safetyArea.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(safetyArea)
if #available(iOS 11, *) {
let guide = view.safeAreaLayoutGuide
safetyArea.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
safetyArea.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
safetyArea.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
safetyArea.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
} else {
safetyArea.topAnchor.constraint(equalTo: topLayoutGuide.topAnchor).isActive = true
safetyArea.bottomAnchor.constraint(equalTo: bottomLayoutGuide.bottomAnchor).isActive = true
safetyArea.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
safetyArea.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
}
}
func setView() {
safetyArea.addSubview(myButtonTop)
myButtonTop.snp.makeConstraints { (make) in
make.top.centerX.equalTo(safetyArea)
}
}
}
'iOS' 카테고리의 다른 글
[Swift] Google Admob 추가 (0) | 2021.12.24 |
---|---|
[Swift] modalPresentationStyle (0) | 2021.12.24 |
[Swift] SnapKit (0) | 2021.12.15 |
[Swift] Lazy 변수 (0) | 2021.12.15 |
[Swift] Storyboard 없이 코드로 프로젝트 시작하기 (0) | 2021.12.11 |