动态地在UIstackView中排列UILabel

时间:2016-06-27 16:13:00

标签: ios autolayout uilabel uistackview

我使用arrangeSubView属性将标签作为子视图添加到UIstackview。

如果我将Stackview轴保持水平,会发生什么 我得到一个UILabels水平条带

label1 label2 label3 label4.....

如果我保持垂直,我会得到一个UILabels的垂直条带 如下所示

label1
label2
label3
and so on

但我想要实现的是这个

label1, label2(if it fits) otherwise take it below
label3, label4
label5 and so on

目前我的代码是这个

 uiLabel1.numberOfLines = 1
            uiLabel1.backgroundColor = THEMECOLOR
            uiLabel1.font = UIFont.preferredFontForTextStyle(UIFontTextStyleCaption1)
            uiLabel1.textColor = topicColor
            uiLabel1.layer.cornerRadius = (CGFloat)(5.0)
            uiLabel1.layer.masksToBounds = true
            uiLabel1.sizeToFit()
            uiLabel1.userInteractionEnabled = true           


            self.dynamicStackView.addArrangedSubview(uiLabel1)

1 个答案:

答案 0 :(得分:1)

我相信UICollectionView的流程布局是您所需要的。请查看链接here