使用字符串数组使用UIButtons填充UIStackView

时间:2018-12-14 15:18:11

标签: swift uistackview

我正在尝试使用一些UIStackView创建一个UIButton

我有一个包含按钮文本的字符串数组。我正在尝试映射该数组,创建一个按钮数组。

然后我可以在该数组上使用forEach来添加arrangedSubView

目前我只看到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
    }

1 个答案:

答案 0 :(得分:1)

错误在这里:

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

应为addArrangedSubview;)

一点点说明,您可以在String数组上执行each,在此处创建按钮并将其添加到UIStackView。这样就避免了一次迭代。