【问题标题】:Set up height constraint for superview based on child views基于子视图为超级视图设置高度约束
【发布时间】:2016-02-02 21:04:18
【问题描述】:

如何为超级视图设置高度约束,其中超级视图的高度是其子视图的总高度,并且它的子视图可以是多行 UILabel 的集合(可以根据字体大小或行数改变高度正在显示文本)。

【问题讨论】:

    标签: objective-c ios7 autolayout xcode7 nslayoutconstraint


    【解决方案1】:

    您可以通过将第一个标签的顶部约束到自身的顶部,然后将最后一个标签的底部约束到自身的底部来做到这一点。中间的每个标签的顶部都限制在前一个标签的底部。

    @interface SomeView: UIView
    @end
    
    @implementation SomeView
    
    - (instancetype)init {
        self = [super init];
        if (!self)
            return nil;
    
        // create an array of random labels
        NSArray <UILabel*> *labels = [self labelsWithRandomText:10];
    
        // add each label to the view, setting it's top to the previous view's bottom
        UIView *previousView = self;
        for (UILabel *label in labels) {
            [self addSubview:label];
    
            // bound the left and right of the label to match the parent view
            [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[label]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]];
    
            // if this is the first label being added, set its top to self's top
            // otherwise set its top to the bottom of the previous view
            NSLayoutAttribute previousAttribute = previousView == self ? NSLayoutAttributeTop : NSLayoutAttributeBottom;
            [self addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousView attribute:previousAttribute multiplier:1 constant:0]];
    
            // update previous view
            previousView = label;
        }
    
        // constrain self's bottom to the bottom of the last label
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:previousView attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
    
        return self;
    }
    
    // create an array of labels w/ random color and background text
    - (NSArray <UILabel *> *)labelsWithRandomText:(int)labelCount {
        NSMutableArray <UILabel*> *labels = [NSMutableArray new];
        for (int i = 0; i < labelCount; i++) {
            UILabel *label = [UILabel new];
            label.translatesAutoresizingMaskIntoConstraints = NO;
            label.numberOfLines = 0;
            label.backgroundColor = [self randomColor];
            label.text = [self randomString];
            [labels addObject:label];
        }
    
        return labels;
    }
    
    // create random color
    - (UIColor *)randomColor {
        return [UIColor colorWithRed:(arc4random()%255)/255. green:(arc4random()%255)/255. blue:(arc4random()%255)/255. alpha:1];
    }
    
    // create random string
    - (NSString *)randomString {
        static NSArray <NSString*> *words;
        static NSInteger wordCount;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            words = @[@"cat ", @"dog ", @"bird ", @"went ", @"home ", @"with ", @"under ", @"red ", @"blue ", @"green "];
            wordCount = [words count];
        });
    
        NSMutableString *randomString = [NSMutableString new];
    
        // create somewhere between 10-30 words
        int randomWordCount = arc4random() % 20 + 10;
        for (int i = 0; i < randomWordCount; i++) {
            [randomString appendString:words[arc4random() % wordCount]];
        }
        return randomString;
    }
    
    @end
    

    【讨论】:

    • 它没有展开
    猜你喜欢
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多