美文网首页
iOS基于UIKit实现的简易弹性盒子布局SNFlexView

iOS基于UIKit实现的简易弹性盒子布局SNFlexView

作者: 迷路的小小 | 来源:发表于2023-03-19 09:37 被阅读0次

SNFlexView是基于UIKit@resultBuilder实现的简易的弹性盒子布局(仅支持自动布局)。

  1. 用法
  • resultBuilder
let flexView = SNFlexView(lineSpacing: 5, interitemSpacing: 5, contents: {
    for str in contentStr {
        if str.count > 5 {
            UILabel(text: str)
                .textColor(.white)
                .padding(UIEdgeInsets(top: 2, left: 5, bottom: 2, right: 5))
                .backgroundColor(.red)
                .cornerRadius(5)
                .masksToBounds(true)
        }
        else {
            UILabel(text: str)
                .textColor(.white)
                .padding(UIEdgeInsets(top: 2, left: 5, bottom: 2, right: 5))
                .backgroundColor(.blue)
                .cornerRadius(5)
                .masksToBounds(true)
        }
    }
})
  • view数组
@SNViewBuilder
var contents: [UIView] {
    for str in contentStr {
        UILabel(text: str)
            .textColor(.white)
            .padding(UIEdgeInsets(top: 2, left: 5, bottom: 2, right: 5))
            .backgroundColor(.red)
            .cornerRadius(5)
            .masksToBounds(true)
    }
}
let flexView = SNFlexView(lineSpacing: 5, interitemSpacing: 5, contents: contents)
  1. 参数说明
  • lineSpacing:行间距;
  • interitemSpacing:列间距;
  • contents:盒子内部子视图;
  1. 效果图


    简易弹性盒子

相关文章

网友评论

      本文标题:iOS基于UIKit实现的简易弹性盒子布局SNFlexView

      本文链接:https://www.haomeiwen.com/subject/seaerdtx.html