Skip to content

使用FlowLayout的标签流,FlowLayout 添加到 UIStackView 上,无法撑开 UIStackView 高度 #161

@xjh093

Description

@xjh093

代码:

    UIStackView *mainStackView = [[UIStackView alloc] init];
    mainStackView.axis = UILayoutConstraintAxisVertical;
    mainStackView.spacing = 20;
    mainStackView.alignment = UIStackViewAlignmentFill;
    mainStackView.distribution = UIStackViewDistributionFill;
    
    MyFlowLayout *flowLayout = [[MyFlowLayout alloc] initWithOrientation:MyOrientation_Vert arrangedCount:0];
    flowLayout.subviewSpacing = 8;
    flowLayout.wrapContentHeight = YES;
    flowLayout.backgroundColor = [UIColor orangeColor];
    
    for (NSInteger i = 0; i < 5; i++) {
        NSArray *titles = @[
            @"123",
            @"ABCDEF",
            @"这里可以看到尺",
            @"宽度等于自己的尺寸",
            @"则按钮的宽度就是内容的宽度",
        ];
        NSInteger index = arc4random()%titles.count;
        
        UIButton *tagButton = [UIButton new];
        [tagButton setTitle:titles[index] forState:UIControlStateNormal];
        tagButton.layer.cornerRadius = 15;
        tagButton.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:((arc4random()%10)*0.1+0.05)];
        tagButton.widthSize.equalTo(@(MyLayoutSize.wrap)).add(10).min(40);
        tagButton.heightSize.equalTo(@(MyLayoutSize.wrap)).add(10);
        [tagButton sizeToFit];
        
        [flowLayout addSubview:tagButton];
    }
    
    [mainStackView addArrangedSubview:flowLayout];

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions