ios - ios swift flexlayout在添加项目后获取flex的高度
问题描述
以下是我在 flex 布局中添加项目的代码https://github.com/layoutBox/FlexLayout
rootFlexContainer.flex.direction(.row).wrap(.wrap).alignSelf(.auto).justifyContent(.start).paddingRight(2).define { (flex) in
for i in 0..<((retailerTags?.count ?? 0) > 3 ? 3 : (retailerTags?.count ?? 0)) {
let nameLabel = UIButton()
nameLabel.isUserInteractionEnabled = false
nameLabel.setTitle((retailerTags?[i] ?? "").trim(), for: .normal)
nameLabel.setTitleColor(.black, for: .normal)
nameLabel.titleLabel?.font = FontStyle.ProximaNovaRegular(size: 11)
nameLabel.contentEdgeInsets = UIEdgeInsets(top: 1.5, left: 4, bottom: 1.5, right:4)
nameLabel.layer.borderColor = UIColor.hexStringToUIColor(hex: TravelXStrings.grayBorderColor).cgColor
nameLabel.layer.cornerRadius = 8
nameLabel.layer.borderWidth = 1.0
flex.addItem(nameLabel).margin(2)
}
if cashbackString != "" {
let cashbackLabel = UIButton()
cashbackLabel.backgroundColor = UIColor.hexStringToUIColor(hex: TravelXStrings.orangeCashbackColor)
cashbackLabel.isUserInteractionEnabled = false
cashbackLabel.setTitle(cashbackString, for: .normal)
cashbackLabel.setTitleColor(.black, for: .normal)
cashbackLabel.titleLabel?.font = FontStyle.ProximaNovaRegular(size: 10)
cashbackLabel.contentEdgeInsets = UIEdgeInsets(top: 1.5, left: 5, bottom: 1.5, right: 5)
cashbackLabel.layer.cornerRadius = 8
cashbackLabel.layer.borderWidth = 0
flex.addItem(cashbackLabel).margin(2)
}
}
添加视图后,我无法在尝试使用bounds.height、flex.intrinsicSize.height添加元素后不久获得此 rootflex 容器的高度,但高度返回错误,仅在方法 layoutSubviews() 中获取精确高度
override func layoutSubviews() {
super.layoutSubviews()
layout()
}
fileprivate func layout() {
rootFlexContainer.frame.size.width = frame.width - 20
rootFlexContainer.flex.layout(mode: .adjustHeight)
if(rootFlexContainer.frame.height ! = 0){
tagsHeight.constant = rootFlexContainer.frame.height
}
//tagsView.flex.layout(mode: .adjustWidth)
}
如何在调用layoutSubviews之前和添加元素后不久获得准确的 flex 布局高度?
解决方案
推荐阅读
- openldap - 让 openLDAP 与组和 memberOf 一起工作
- json - componentDidMount() 获取本地数据
- mysql - 请sql查询,我试过了,但我无法得到它
- python - 如何仅显示 Pandas 数据框中的某些列?
- git - 关于 Git 和 GitKraken 的一些新手问题
- c++ - 编译器优化:将变量从堆栈移动到寄存器
- java - 如何使用 Selenium 和 Java 单击鼠标悬停在 ebay.com 内的元素上后可见的元素
- java - 如何检查未设置的对象是否具有属性
- sql-server - ADO.NET 多个连接字符串无法找到本地数据库
- ruby-on-rails - Rails 在渲染良好且没有任何解释后返回 500 状态