Evenly space multiple views within a container view

后端 未结 29 3061
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 06:14

Auto Layout is making my life difficult. In theory, it was going to be really useful when I switched, but I seem to fight it all of the time.

I\'ve made a demo proje

29条回答
  •  被撕碎了的回忆
    2020-11-22 06:35

    I've been on a rollercoaster ride of loving autolayout and hating it. The key to loving it seems to be to accept the following:

    1. Interface builder's editing and "helpful" auto-creation of constraints is near useless for all but the most trivial case
    2. Creating categories to simplify common operations is a life-saver since the code is so repetitive and verbose.

    That said, what you are attempting is not straightforward and would be difficult to achieve in interface builder. It is pretty simple to do in code. This code, in viewDidLoad, creates and positions three labels how you are asking for them:

    // Create three labels, turning off the default constraints applied to views created in code
    UILabel *label1 = [UILabel new];
    label1.translatesAutoresizingMaskIntoConstraints = NO;
    label1.text = @"Label 1";
    
    UILabel *label2 = [UILabel new];
    label2.translatesAutoresizingMaskIntoConstraints = NO;
    label2.text = @"Label 2";
    
    UILabel *label3 = [UILabel new];
    label3.translatesAutoresizingMaskIntoConstraints = NO;
    label3.text = @"Label 3";
    
    // Add them all to the view
    [self.view addSubview:label1];
    [self.view addSubview:label2];
    [self.view addSubview:label3];
    
    // Center them all horizontally
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label2 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label3 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    
    // Center the middle one vertically
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label2 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
    
    // Position the top one half way up
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:label2 attribute:NSLayoutAttributeCenterY multiplier:0.5 constant:0]];
    
    // Position the bottom one half way down
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label3 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:label2 attribute:NSLayoutAttributeCenterY multiplier:1.5 constant:0]];
    

    As I say, this code is much simplified with a couple of category methods in UIView, but for clarity I've done it the long way here.

    The category is here for those interested, and it has a method for evenly spacing an array of views along a particular axis.

提交回复
热议问题