1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VFL でビューを真ん中に寄せる Autolayout 制約を作る

Last updated at Posted at 2016-01-27

そもそもサイズの小さなビューをセンターに寄せるのは Visual Format Language(VFL) では単純ではない。

VFL と + constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant: を組み合わせる

簡単に説明すると、

  1. VFL で問題のビューのサイズだけを定義する
  2. constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant: でスーパービューに対して真ん中に寄せる制約を作る

VFL だけではできない

AlignAllCenterX AlignAllCenterY を使用する

幅に対して真ん中にしたいなら、 "V:[subjectView]" として AlignAllCenterX を指定する。高さなら "H:[subjectView]" にして AlignAllCenterY を指定。

ただしこちらは未確認。

スペーサーのビューを挿入する

上記は真ん中に寄せたいビューが一つの場合にしか通用しない。真ん中に小さいビューを2つ以上配置したい場合は、スペーサーのビューを配置する。

"H:|[spacerLeft]-[subjectView(100)]-[subjectView2(50)]-[spacerRight(==spacerLeft)]|"

コードが汚くなりがちだが、適用可能な範囲が広いと思う。

まとめ

一長一短

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?