LoginSignup
2
2

More than 5 years have passed since last update.

iOS AutoLayoutについて(Storyboard・Code)

Last updated at Posted at 2015-11-30

AutolayoutとはviewにConstraint(制約)を付けることでレイアウトする方法。

下記の図に簡単にまとめってみました。

autolayout2.png

Sample Code:
Fix size to 100X100 and distance 30X30;

    [self addSubview:self.testview];
    NSArray *c_v = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[testview(==100)]"
                                                           options:0
                                                           metrics:nil
                                                             views:@{@"testview":self.testview}];

    NSArray *c_h = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[testview(==100)]"
                                                           options:0
                                                           metrics:nil
                                                             views:@{@"testview":self.testview}];

    [self addConstraints:c_h];
    [self addConstraints:c_v];

    NSArray *l_v = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-hdistance-[testview]"
                                                           options:0
                                                           metrics:@{@"hdistance":@(30)}
                                                             views:@{@"testview":self.testview}];

    NSArray *l_h = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[testview]-vdistance-|"
                                                           options:0
                                                           metrics:@{@"vdistance":@(30)}
                                                             views:@{@"testview":self.testview}];

    [self addConstraints:l_v];
    [self addConstraints:l_h];

参考資料:
http://dev.classmethod.jp/references/ios-7-xcode-5-auto-layout-2/

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