swift - 使用字符串数组用 UIButtons 填充 UIStackView
问题描述
我正在尝试UIStackView
用一些UIButton
's 创建一个。
我有一个包含按钮文本的字符串数组。我试图映射该数组,创建一个按钮数组。
然后我可以forEach
在该数组上使用添加arrangedSubView
's.
目前我只看到 1 个按钮,我怀疑我的按钮在循环的每一步都被覆盖了。
fileprivate var button: UIButton {
let button = UIButton(type: UIButton.ButtonType.system)
return button
}
fileprivate let buttonGroupStackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .vertical
stackView.distribution = .equalSpacing
return stackView
}()
fileprivate func setupSubViews(_ origin: ChatResponseOrigin) {
let margins = contentView.layoutMarginsGuide
[messageAvatar, buttonGroupStackView].forEach { v in contentView.addSubview(v) }
messageAvatar.image = #imageLiteral(resourceName: "user-avatar")
messageAvatar.anchor(
top: margins.topAnchor, leading: margins.leadingAnchor, size: CGSize(width: 35, height: 35)
)
guard let buttonGroupContent = content?.buttonGroup else { return }
let buttonGroup = buttonGroupContent.map { (b) -> UIButton in
let btn = button
btn.frame = CGRect(x: 0, y: 0, width: 200, height: 40)
btn.backgroundColor = UIColor.blue
btn.setTitle(b.buttonText, for: .normal)
return btn
}
buttonGroup.forEach { b in buttonGroupStackView.addSubview(b) }
buttonGroupStackView.anchor(top: margins.topAnchor, leading: margins.leadingAnchor, trailing: margins.trailingAnchor, size: CGSize(width: 0, height: 400))
}
我有一个扩展,UIView
用于处理自动布局
@discardableResult
func anchor(top: NSLayoutYAxisAnchor? = nil, leading: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, trailing: NSLayoutXAxisAnchor? = nil, padding: UIEdgeInsets = .zero, size: CGSize = .zero) -> AnchoredConstraints {
translatesAutoresizingMaskIntoConstraints = false
var anchoredConstraints = AnchoredConstraints()
if let top = top {
anchoredConstraints.top = topAnchor.constraint(equalTo: top, constant: padding.top)
}
if let leading = leading {
anchoredConstraints.leading = leadingAnchor.constraint(equalTo: leading, constant: padding.left)
}
if let bottom = bottom {
anchoredConstraints.bottom = bottomAnchor.constraint(equalTo: bottom, constant: -padding.bottom)
}
if let trailing = trailing {
anchoredConstraints.trailing = trailingAnchor.constraint(equalTo: trailing, constant: -padding.right)
}
if size.width != 0 {
anchoredConstraints.width = widthAnchor.constraint(equalToConstant: size.width)
}
if size.height != 0 {
anchoredConstraints.height = heightAnchor.constraint(equalToConstant: size.height)
}
[anchoredConstraints.top, anchoredConstraints.leading, anchoredConstraints.bottom, anchoredConstraints.trailing, anchoredConstraints.width, anchoredConstraints.height].forEach { $0?.isActive = true }
return anchoredConstraints
}
解决方案
错误在这里:
buttonGroup.forEach { b in buttonGroupStackView.addSubview(b) }
应该是addArrangedSubview
;)
少说一句,你可以each
在你的字符串数组上做,在那里创建按钮并将其添加到UIStackView
. 所以你避免了一次迭代。
推荐阅读
- json - 深度嵌套 obj 的 Normalizr 用法
- java - 如何避免在使用 Maps 时重复使用 put() 方法?
- c# - 为什么 GenerateFunctions 找不到 System.Runtime, Version=4.2.1.0 并抛出 FileNotFoundException)?
- ansible - Jinja2 语法错误:预期标记“打印语句结束”
- reactjs - Redux 未采取行动
- tensorflow - 张量流中的常规模型检查点和保存的模型有什么区别?
- raspberry-pi - RaspberryPi 上的 Pharo:启动时未找到模块
- r - 如果字符串与向量中的最后一个和下一个不同,则替换它
- php - PHP 标头重定向,如果变量则重定向
- r - R:在 R 函数中使用 Lubridate ymd() 时出现日期解析错误,相同的代码在我编写的函数之外工作