Things take time

[SWIFT] 오토레이아웃 코드로 본문

iOS (기능)

[SWIFT] 오토레이아웃 코드로

겸손할 겸 2017. 10. 27. 10:21

[Code]

containerView = UIView(frame: self.view.frame) self.view.addSubview(containerView) //제약조건을 프로그래밍으로 할때는 뷰 자체적으로 수행하는 오토리사이징을 꺼야 함(이유 : 사용자가 지정한 오토레이아웃 조건과 충돌하여 제약조건 문제를 일으킴) containerView.translatesAutoresizingMaskIntoConstraints = false self.view.addConstraint(NSLayoutConstraint(item: containerView, attribute: .top, relatedBy: .equal, toItem: self.topLayoutGuide, attribute: .bottom, multiplier: 1, constant: 0)) self.view.addConstraint(NSLayoutConstraint(item: containerView, attribute: .bottom,relatedBy: .equal, toItem: self.bottomLayoutGuide, attribute: .bottom, multiplier: 1, constant: 0)) self.view.addConstraint(NSLayoutConstraint(item: containerView, attribute: .left ,relatedBy: .equal, toItem: self.view, attribute: .left , multiplier: 1, constant: 0)) self.view.addConstraint(NSLayoutConstraint(item: containerView, attribute: .right ,relatedBy: .equal, toItem: self.view, attribute: .right , multiplier: 1, constant: 0))

일단 뷰에 addSubView로 추가를 하고 이후에 코드로 작성할 것

위의 코드는 소스를 보면 알겠지만 화면의 크기에 맞춰 뷰를 늘린 것, topLayoutGuide까지 해야 상태바 밑으로 나오게 됨



[추가]


특정 뷰에 대비하여 제약조건을 걸 때는 위의 코드이나, 현재 하나의 뷰의 너비, 높이만 조절하고싶다면 아래와 같이 작성한다.

NSLayoutConstraint(item: ivMedia, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: size.height)