Evenly space multiple views within a container view

后端 未结 29 2861
佛祖请我去吃肉
佛祖请我去吃肉 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:50

    I know it's been a while since the first answer, but I just came across the very same problem and I want to share my solution. For generations to come...

    I set my views on viewDidLoad:

    - (void)viewDidLoad {
    
        [super viewDidLoad];
    
        cancelButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
        cancelButton.translatesAutoresizingMaskIntoConstraints = NO;
        [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
        [self.view addSubview:cancelButton];
    
        middleButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
        middleButton.translatesAutoresizingMaskIntoConstraints = NO;
        [middleButton setTitle:@"Middle" forState:UIControlStateNormal];
        [self.view addSubview:middleButton];
    
        nextButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
        nextButton.translatesAutoresizingMaskIntoConstraints = NO;
        [nextButton setTitle:@"Next" forState:UIControlStateNormal];
        [self.view addSubview:nextButton];
    
    
        [self.view setNeedsUpdateConstraints];
    
    }
    

    And then, on updateViewConstrains, first I delete all constrains, then I create the views dictionary and then I calculate the space to be used between views. After that, I just use the Visual Language Format to set the constraints:

    - (void)updateViewConstraints {
    
    
        [super updateViewConstraints];
    
        [self.view removeConstraints:self.view.constraints];
    
        NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(cancelButton, nextButton, middleButton);
    
        float distance=(self.view.bounds.size.width-cancelButton.intrinsicContentSize.width-nextButton.intrinsicContentSize.width-middleButton.intrinsicContentSize.width-20-20)/  ([viewsDictionary count]-1);  // 2 times 20 counts for the left & rigth margins
        NSNumber *distancies=[NSNumber numberWithFloat:distance];
    
    //    NSLog(@"Distancies: %@", distancies);
    //    
    //    NSLog(@"View Width: %f", self.view.bounds.size.width);
    //    NSLog(@"Cancel Width: %f", cancelButton.intrinsicContentSize.width);
    //    NSLog(@"Middle Width: %f", middleButton.intrinsicContentSize.width);
    //    NSLog(@"Next Width: %f", nextButton.intrinsicContentSize.width);
    
    
    
        NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[cancelButton]-dis-[middleButton]-dis-[nextButton]-|"
                                                                       options:NSLayoutFormatAlignAllBaseline
                                                                       metrics:@{@"dis":distancies}
                                                                         views:viewsDictionary];
    
    
        [self.view addConstraints:constraints];
    
    
    
        constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[nextButton]-|"
                                                              options:0
                                                              metrics:nil
                                                                views:viewsDictionary];
        [self.view addConstraints:constraints];
    
    
    
    }
    

    The good thing about this method is that you have to do very little math. I'm not saying this is the perfect solution, but I works for the layout I was trying to achieve.

    I hope it helps.

提交回复
热议问题