首页 > 解决方案 > 使用字符串数组用 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
    }

标签: swiftuistackview

解决方案


错误在这里:

    buttonGroup.forEach { b in buttonGroupStackView.addSubview(b) }

应该是addArrangedSubview;)

少说一句,你可以each在你的字符串数组上做,在那里创建按钮并将其添加到UIStackView. 所以你避免了一次迭代。


推荐阅读