Evenly space multiple views within a container view

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

    I have made a function that might help. This usage example :

     [self.view addConstraints: [NSLayoutConstraint fluidConstraintWithItems:NSDictionaryOfVariableBindings(button1, button2, button3)
                                                                    asString:@[@"button1", @"button2", @"button3"]
                                                                   alignAxis:@"V"
                                                              verticalMargin:100
                                                            horizontalMargin:50
                                                                 innerMargin:25]];
    

    will cause that vertical distribution (sorry don't have the 10 reputation to embed images). And if you change the axis and some margin values :

    alignAxis:@"H"
    verticalMargin:120
    horizontalMargin:20
    innerMargin:10
    

    You'll get that horizontal distribution.

    I'm newbie in iOS but voilà !

    EvenDistribution.h

    @interface NSLayoutConstraint (EvenDistribution)
    
    /**
     * Returns constraints that will cause a set of subviews
     * to be evenly distributed along an axis.
     */
    + (NSArray *)  fluidConstraintWithItems:(NSDictionary *) views
                                   asString:(NSArray *) stringViews
                                  alignAxis:(NSString *) axis
                             verticalMargin:(NSUInteger) vMargin
                           horizontalMargin:(NSUInteger) hMargin
                                innerMargin:(NSUInteger) inner;
    @end
    

    EvenDistribution.m

    #import "EvenDistribution.h"
    
    @implementation NSLayoutConstraint (EvenDistribution)
    
    + (NSArray *) fluidConstraintWithItems:(NSDictionary *) dictViews
                                  asString:(NSArray *) stringViews
                                 alignAxis:(NSString *) axis
                            verticalMargin:(NSUInteger) vMargin
                          horizontalMargin:(NSUInteger) hMargin
                               innerMargin:(NSUInteger) iMargin
    
    {
        NSMutableArray *constraints = [NSMutableArray arrayWithCapacity: dictViews.count];
        NSMutableString *globalFormat = [NSMutableString stringWithFormat:@"%@:|-%d-",
                                         axis,
                                         [axis isEqualToString:@"V"] ? vMargin : hMargin
                                         ];
    
    
    
            for (NSUInteger i = 0; i < dictViews.count; i++) {
    
                if (i == 0)
                    [globalFormat appendString:[NSString stringWithFormat: @"[%@]-%d-", stringViews[i], iMargin]];
                else if(i == dictViews.count - 1)
                    [globalFormat appendString:[NSString stringWithFormat: @"[%@(==%@)]-", stringViews[i], stringViews[i-1]]];
                else
                   [globalFormat appendString:[NSString stringWithFormat: @"[%@(==%@)]-%d-", stringViews[i], stringViews[i-1], iMargin]];
    
                NSString *localFormat = [NSString stringWithFormat: @"%@:|-%d-[%@]-%d-|",
                                         [axis isEqualToString:@"V"] ? @"H" : @"V",
                                         [axis isEqualToString:@"V"] ? hMargin : vMargin,
                                         stringViews[i],
                                         [axis isEqualToString:@"V"] ? hMargin : vMargin];
    
                [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:localFormat
                                                                                         options:0
                                                                                         metrics:nil
                                                                                           views:dictViews]];
    
    
        }
        [globalFormat appendString:[NSString stringWithFormat:@"%d-|",
                                    [axis isEqualToString:@"V"] ? vMargin : hMargin
                                    ]];
    
        [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:globalFormat
                                                                                 options:0
                                                                                 metrics:nil
                                                                                   views:dictViews]];
    
        return constraints;
    
    }
    
    @end
    

提交回复
热议问题