코드로 AutoLayout을 제어하려다 도저히 이해하기 어려워 StoryBoard로 한번 만들어 코드로 교체했다.

        sv.contentSize=CGSizeMake(100, 1500)
        sv.backgroundColor=UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0)
        let v1:UIView = UIView()
        sv.addSubview(v1);
        v1.backgroundColor=UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
        v1.frame=CGRectMake(10, 100, 100, 300);
↑ AutoLayout 사용하지 않음
        sv.contentSize=CGSizeMake(100, 1500)
        sv.backgroundColor=UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0)
        let v1:UIView = UIView()
        sv.addSubview(v1);
        v1.backgroundColor=UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
        v1.setTranslatesAutoresizingMaskIntoConstraints(false)
        v1.addConstraint(NSLayoutConstraint(item: v1, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: 300.0))
        v1.addConstraint(NSLayoutConstraint(item: v1, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1.0, constant: 100.0))
        sv.addConstraint(NSLayoutConstraint(item: v1, attribute: .Top, relatedBy: .Equal, toItem: sv, attribute: .Top, multiplier: 1.0, constant: 100.0))
        sv.addConstraint(NSLayoutConstraint(item: v1, attribute: .Bottom, relatedBy: .Equal, toItem: sv, attribute: .Bottom, multiplier: 1.0, constant: 10.0))
        sv.addConstraint(NSLayoutConstraint(item: v1, attribute: .Leading, relatedBy: .Equal, toItem: sv, attribute: .Leading, multiplier: 1.0, constant: 10.0))
        sv.addConstraint(NSLayoutConstraint(item: v1, attribute: .Trailing, relatedBy: .Equal, toItem: sv, attribute: .Trailing, multiplier: 1.0, constant: 10.0))
↑ AutoLayout 사용
이것도 처음엔 몰랐어,sv.addConstraint를 만드는 곳 v1.망했어, addConstraint.
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint: view:>'
*** First throw call stack:
(0x33a4b2a3 0x3b6c997f 0x33a4b1c5 0x34408ad5 0x35c8c3c1 0x35c8c535 0x358732e1 0x358735c7 0x3586de53 0x358557dd 0x358552c3 0x358c9ce3 0x358c9afb 0x358c6ceb 0x358c64c1 0x358b4b93 0x358b4833 0x2eed47 0x2e2eb9 0x2e6159 0x20b279 0x35868ab3 0x358dd8ef 0x35612c01 0x3bae04b7 0x3bae1dcb 0x33a1ef3b 0x33991ebd 0x33991d49 0x375442eb 0x358a7301 0xc4bc5 0x3bb00b20)
libc++abi.dylib: terminate called throwing an exception
그런 말.

좋은 웹페이지 즐겨찾기