そもそもサイズの小さなビューをセンターに寄せるのは Visual Format Language(VFL) では単純ではない。
VFL と + constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:
を組み合わせる
簡単に説明すると、
- VFL で問題のビューのサイズだけを定義する
-
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)]|"
コードが汚くなりがちだが、適用可能な範囲が広いと思う。
まとめ
一長一短