Autolayout으로 아이의 크기에 맞게 신축할 수 있는 상자를 만들고 싶어요.
5857 단어 iOS
하고 싶은 거.
신축1:2
신축1:3
방법
외부 뷰에 적절한 제한
이어서 아이뷰의 제약이 더해졌다.
4주간의 Mergin을 0으로 확장하고 Apsect 비율을 낮은 우선 순위로 설정합니다.
class ViewController: UIViewController {
@IBOutlet weak var innerContentView: UIView!
private var touched: Bool = false
private var constraint = NSLayoutConstraint()
@IBAction func buttonDidTouchUped(sender: AnyObject) {
touched = !touched
let ratio = touched ? CGFloat(2.0) : CGFloat(3.0)
updateConstraint(ratio)
}
func updateConstraint(ratio: CGFloat) {
innerContentView.removeConstraint(constraint)
constraint = NSLayoutConstraint(
item: innerContentView,
attribute: NSLayoutAttribute.Height,
relatedBy: NSLayoutRelation.Equal,
toItem: innerContentView,
attribute: NSLayoutAttribute.Width,
multiplier: ratio,
constant: CGFloat(0.0)
)
constraint.priority = 501
innerContentView.addConstraints([ constraint ])
innerContentView.updateConstraints()
}
}
여기에 초기치가 설정한 priority보다 높은 priority를 추가하고updateConstraints()
는 서브뷰의 크기를 동태적으로 반영하며 이에 따라 부모의 뷰의 높이가 신축된다.코드
Reference
이 문제에 관하여(Autolayout으로 아이의 크기에 맞게 신축할 수 있는 상자를 만들고 싶어요.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/mattak/items/ac3e68cee34e16f92514텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)