본문 바로가기
앱/iOS(Swift)

[Swift] 코드로 뷰추가해보기( autolayout )

by 리드맥 2022. 3. 27.
class ViewController: UIViewController {

    let redView = UIView()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.addSubview(redView)
        redView.translatesAutoresizingMaskIntoConstraints = false
        redView.backgroundColor = .gray
        
        NSLayoutConstraint.activate(
        [
            redView.topAnchor.constraint(equalTo: self.view.topAnchor),
            redView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
            redView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
            redView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
        ])

    }
}

주의점!!!!: 먼저 view에 추가하고 제약을 걸어야함!

댓글