LoginSignup
1
1

More than 5 years have passed since last update.

[iOS] AutoLayoutでViewを上下中央配置にする。

Last updated at Posted at 2016-08-22

AutoLayoutでViewを上下中央配置にする。
(※ self.view:親 self.fooview:子)

// AutoLayoutの自動変換をOFF
[self.fooview setTranslatesAutoresizingMaskIntoConstraints:NO];

// X方向の制約
NSLayoutConstraint *xConstraint =
 [NSLayoutConstraint constraintWithItem:self.fooview
                              attribute:NSLayoutAttributeCenterX
                              relatedBy:NSLayoutRelationEqual
                                 toItem:self.view
                              attribute:NSLayoutAttributeCenterX
                             multiplier:1.0                                            
                               constant:0];
// Y方向の制約
NSLayoutConstraint *yConstraint =
 [NSLayoutConstraint constraintWithItem:self.fooview
                              attribute:NSLayoutAttributeCenterY
                              relatedBy:NSLayoutRelationEqual
                                 toItem:self.view
                              attribute:NSLayoutAttributeCenterY
                             multiplier:1.0
                               constant:0];
[self.view addConstraint:xConstraint];
[self.view addConstraint:yConstraint];
1
1
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
1