LoginSignup
8
8

More than 5 years have passed since last update.

ざっくりコードでAutoLayout

Posted at

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];

8
8
0

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
8
8