LoginSignup
11
7

More than 5 years have passed since last update.

AutoLayoutをコードで制御したい時、どうもわかりにくいのでStoryBoardで一度作ってコードに置き換えている。

Last updated at Posted at 2015-03-03

xx.png

        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

とか言われたりした。

11
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
7