如何将 UIButtons 数组添加到 UIStackview 上?

How to add an array of UIButtons onto a UIStackview?

我使用字母表中的 26 个字母并用相应的字母创建 UIButton,在创建 UIButtons 的数组后,我发送存储这些按钮的数组,然后我将其添加到堆栈视图。

stackview 也在我称为 abcBtnViewUIView 中。

如果我将整个 UIButton 数组传递给它,它就可以工作,但是垂直或水平 26 个按钮看起来不太好。所以我决定不发送 26 个按钮的数组,而是发送 6 个数组,5 个有 5 个 UIbuttons 和一个有 1 个按钮的数组。

我得到的错误是 我无法将 UIButton 转换为类型 UIView

参数UIStackview(arrangedSubviews: [UIView])的类型是[UIView],但是它第一次还是占用了我的[UIButton]数组,但如果我放置了多个数组,它就不会占用它UIbuttons.

我想知道如何将多个 UIButton 数组添加到 UIStackview,以便我可以拥有 6 列和 5 行的 UIButton?

我尝试过的事情:

  1. 尝试将 [UIButton] 类型转换为 [UIView]
  2. 创建了一个包含 UIButton 数组的数组,然后使用了一个 for 循环将每个 UIstackview 添加到我们的视图(abcBtnView)中。

下面的代码可以工作,但我只能传入一个 UIBUTTON。

private func makeABCbtns(){

    let abcde = createButtons(named: "A", "B", "C", "D", "E")
    let fghij = createButtons(named: "F", "G", "H", "I", "J")
    let klmno = createButtons(named: "K","L", "M", "N", "O")
    let pqrst = createButtons(named: "P", "Q","R", "S", "T")
    let uvwxy = createButtons(named: "U", "V", "W","X", "Y")
    let z     = createButtons(named: "Z")

    let stackView = UIStackView(arrangedSubviews: abcde)
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.axis = .horizontal
    stackView.spacing = 1
    stackView.distribution = .fillEqually

    // UIView where all the buttons will be in.
    abcBtnView.addSubview(stackView)


    //I am giving the stackview the size of the abcBtnView.
    stackView.anchor(top: abcBtnView.topAnchor,
                     leading: abcBtnView.leadingAnchor,
                     bottom: abcBtnView.bottomAnchor,
                     trailing: abcBtnView.trailingAnchor,
                     centerXaxis: nil,
                     centerYaxis: nil)
}

func createButtons(named: String...) -> [UIButton]{
    return named.map { letter in
        let button = UIButton()
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle(letter, for: .normal)
        button.backgroundColor = .green
        button.setTitleColor( .blue , for: .normal)
        return button
    }

}

听起来您正在尝试创建 UIButton 的网格。这不能用一个 UIStackView 来完成。取而代之的是,您需要在另一个 UIStackView 中放置一堆 UIStackView,这只是为了让您知道这是一个非常糟糕的主意。

或者不用重新发明轮子,您可以使用 UICollectionView,并将按钮布置为网格。每个按钮都在 UICollectionViewCell.

您的代码使用骨架故事板工作,因此问题可能出在您代码的其他地方。这是根据Teja的建议重构的代码。

  private func makeABCbtns(){

    let list = [["A", "B", "C", "D", "E"], ["F", "G", "H", "I", "J"], ["K","L", "M", "N", "O"], ["P", "Q","R", "S", "T"], ["U", "V", "W","X", "Y"], ["Z"]]
    var groups = [UIStackView]()

    for i in list {
      let group = createButtons(named: i)
      let subStackView = UIStackView(arrangedSubviews: group)
      subStackView.axis = .horizontal
      subStackView.distribution = .fillEqually
      subStackView.spacing = 1
      groups.append(subStackView)
    }

    let stackView = UIStackView(arrangedSubviews: groups)
    stackView.axis = .vertical
    stackView.distribution = .fillEqually
    stackView.spacing = 1
    stackView.translatesAutoresizingMaskIntoConstraints = false

    abcBtnView.addSubview(stackView)

    stackView.leadingAnchor.constraint (equalTo: abcBtnView.leadingAnchor,  constant: 0).isActive = true
    stackView.topAnchor.constraint     (equalTo: abcBtnView.topAnchor,      constant: 0).isActive = true
    stackView.trailingAnchor.constraint(equalTo: abcBtnView.trailingAnchor, constant: 0).isActive = true
    stackView.bottomAnchor.constraint  (equalTo: abcBtnView.bottomAnchor,   constant: 0).isActive = true
  }

  func createButtons(named: [String]) -> [UIButton]{
    return named.map { letter in
      let button = UIButton()
      button.translatesAutoresizingMaskIntoConstraints = false
      button.setTitle(letter, for: .normal)
      button.backgroundColor = .green
      button.setTitleColor( .blue , for: .normal)
      return button
    }
  }
}