Evenly space multiple views within a container view

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

    Most of these solutions depend on there being an odd number of items so that you can take the middle item and center it. What if you have an even number of items that you still want to be evenly distributed? Here's a more general solution. This category will evenly distribute any number of items along either the vertical or horizontal axis.

    Example usage to vertically distribute 4 labels within their superview:

    [self.view addConstraints:
         [NSLayoutConstraint constraintsForEvenDistributionOfItems:@[label1, label2, label3, label4]
                                            relativeToCenterOfItem:self.view
                                                        vertically:YES]];
    

    NSLayoutConstraint+EvenDistribution.h

    @interface NSLayoutConstraint (EvenDistribution)
    
    /**
     * Returns constraints that will cause a set of views to be evenly distributed horizontally
     * or vertically relative to the center of another item. This is used to maintain an even
     * distribution of subviews even when the superview is resized.
     */
    + (NSArray *) constraintsForEvenDistributionOfItems:(NSArray *)views
                                 relativeToCenterOfItem:(id)toView
                                             vertically:(BOOL)vertically;
    
    @end
    

    NSLayoutConstraint+EvenDistribution.m

    @implementation NSLayoutConstraint (EvenDistribution)
    
    +(NSArray *)constraintsForEvenDistributionOfItems:(NSArray *)views
                               relativeToCenterOfItem:(id)toView vertically:(BOOL)vertically
    {
        NSMutableArray *constraints = [NSMutableArray new];
        NSLayoutAttribute attr = vertically ? NSLayoutAttributeCenterY : NSLayoutAttributeCenterX;
    
        for (NSUInteger i = 0; i < [views count]; i++) {
            id view = views[i];
            CGFloat multiplier = (2*i + 2) / (CGFloat)([views count] + 1);
            NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:view
                                                                          attribute:attr
                                                                          relatedBy:NSLayoutRelationEqual
                                                                             toItem:toView
                                                                          attribute:attr
                                                                         multiplier:multiplier
                                                                           constant:0];
            [constraints addObject:constraint];
        }
    
        return constraints;
    }
    
    @end
    

提交回复
热议问题