FrameBaseLayout
- FrameBaseLayout + AutoResizing
- ์์ฆ์๋ ํด๋น ๋ฐฉ์์ผ๋ก๋ ๊ธฐ๊ธฐ๋ฅผ ์ ๋ถ ๋์ํ๊ธฐ ์ด๋ ค์์ง
view.addSubview(emailTextField)
// ํฌ๊ธฐ์ ์์น ๋ฐ ์์ฑ ์ ์
emailTextField.frame = CGRect(x: 50, y: 50, width: UIScreen.main.bounds.width - 100, height: 50)
emailTextField.borderStyle = .line
emailTextField.backgroundColor = .lightGray
AutoLayout
- AutoLayout (๊ธฐ๊ธฐ๋ณ ๋์ ํ์) + NSLayoutConstraints Class(- base)
- AutoLayout ์์ ์ AutoResizing False ํ์
- Constraints ์ก๊ธฐ ์ํด isActive, addConstraints, NSLayoutAnchor(iOS 9+) ์ฌ์ฉ
- SafeAreaLayoutGuide(iOS 13+)
1. NSLayoutConstraint
view.addSubview(passwordTextField)
passwordTextField.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: passwordTextField, attribute: .top, relatedBy: .equal, toItem: view.safeAreaLayoutGuide, attribute: .top, multiplier: 1, constant: 100).isActive = true
NSLayoutConstraint(item: passwordTextField, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 50).isActive = true
NSLayoutConstraint(item: passwordTextField, attribute: .trailing, relatedBy: .equal, toItem: emailTextField, attribute: .trailing, multiplier: 1, constant: -50).isActive = true
NSLayoutConstraint(item: passwordTextField, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 60).isActive = true
2. NSConstraints + addConstraints
view.addSubview(passwordTextField)
let top = NSLayoutConstraint(item: passwordTextField, attribute: .top, relatedBy: .equal, toItem: view.safeAreaLayoutGuide, attribute: .top, multiplier: 1, constant: 100)
let leading = NSLayoutConstraint(item: passwordTextField, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 50)
let trailing = NSLayoutConstraint(item: passwordTextField, attribute: .trailing, relatedBy: .equal, toItem: emailTextField, attribute: .trailing, multiplier: 1, constant: -50)
let height = NSLayoutConstraint(item: passwordTextField, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 60)
view.addConstraints([top, leading, trailing, height])
3. NSLayoutAnchor
view.addSubview(signButton)
signButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
signButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
signButton.widthAnchor.constraint(equalToConstant: 300),
signButton.heightAnchor.constraint(equalToConstant: 50),
signButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
])
Adaptive Layout
- ์์ดํฐ, ์์ดํจ๋ ๋์
- ๋คํฌ๋ชจ๋
- ์ฌ๋ผ์ด๋์ค๋ฒ
SnapKit
- SnapKit์ ์คํ ๋ ์ด์์์ ์ฝ๊ฒ ์ก์ ์ ์๋๋ก ๋์์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ๋ด๋ถ์ ์ผ๋ก translatesAutoresizingMaskIntoConstraints๊ฐ false๋ก ๊ตฌํ๋๊ฒ ๋์ด ์์
- ์ ์ฝ ์กฐ๊ฑด์ ์ถฉ๋์ ๋ง๊ธฐ ์ํจ์ด๋ผ๊ณ ๋ณผ ์ ์์
internal func prepare() {
if let view = self as? ConstraintView {
view.translatesAutoresizingMaskIntoConstraints = false
}
}
- ๋ ์์ธํ ๋ฐฉ๋ฒ์ ์ฐพ์๋ณด๊ธฐ ๋ฐ๋
[redView, blackView].forEach {
view.addSubview($0)
}
redView.snp.makeConstraints {
$0.width.height.equalTo(200)
$0.top.equalTo(view.safeAreaLayoutGuide)
$0.centerX.equalTo(view)
}
blackView.snp.makeConstraints {
$0.edges.equalTo(redView).inset(50)
}
'๐ฑ SeSAC' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SeSAC] 220819 TIL (0) | 2022.08.20 |
---|---|
[SeSAC] 220818 TIL (0) | 2022.08.20 |
[SeSAC] 220816 TIL (0) | 2022.08.16 |
[SeSAC] 220812 TIL (0) | 2022.08.15 |
[SeSAC] 220811 TIL (0) | 2022.08.15 |