๐ŸŒฑ SeSAC

[SeSAC] 220817 TIL

taeeekki 2022. 8. 20. 02:00

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)
}