iOSではUIパーツの位置を指定するのに、現在3つの選択肢があります。
1.AutoresizingMask
2.Auto Layout
3.コード
storyboardで後からパーツを足す場合、最初からAutoLayoutしてhiddenばっかしてられないので、追加したパーツに対してコードでAutoLayoutを書く必要性がある。
NSLayoutConstraint のコンストラクタに定義していこう
constraintWithItem 元となるアイテムを指定
attribute 属性指定
relatedBy その関係性を指定NSLayoutRelationEqualでおけ
toItem 相手のアイテムを指定、指定しない場合はnil
attribute 属性指定
multiplier とりあえず1.0をおけ
constant アイテムとの距離or自分のサイズ
例、この場合self(buttonでも何でもおけ)が主役、superViewに対してtopから10離す
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeTop multiplier:1.0
constant:10];
superViewに追加
[superview addConstraint:constraint];