无法在 UIStackView 中设置编程布局约束

Cannot Set Programmatic Layout Constraints in UIStackView

我正在尝试将我通过故事板创建的 UIStackView 中的单选按钮居中。但是,单选按钮是根据 Firebase 中的数据以编程方式在 UIStackView 中创建的。

我正在使用 DLRadioButtons 提供的库。

Label下方的UIStackView,命名为radioStackView:

基于 Firebase 数据添加单选按钮的代码:

  `
    pollRef.observe(FIRDataEventType.value, with: {(snapshot) in
    self.numberOfChildren = Int(snapshot.childSnapshot(forPath: "answers").childrenCount)
    self.passLabel.text = String(self.numberOfChildren)
    print(self.numberOfChildren)

    var buttons = [DLRadioButton]()

      for x in 0..<self.numberOfChildren {
    let answerLabel = snapshot.childSnapshot(forPath: "answers").childSnapshot(forPath: String(x+1)).childSnapshot(forPath: "answer").value
    let firstRadioButton = self.createRadioButton(frame: CGRect(x: 1, y:1 , width: 80.0, height: 1.0), title: answerLabel as! String, color: UIColor.black)
        firstRadioButton.tag = x
        firstRadioButton.translatesAutoresizingMaskIntoConstraints = false
        buttons.append(firstRadioButton)
        let margins = self.radioStackView.layoutMarginsGuide
        firstRadioButton.topAnchor.constraint(equalTo: margins.topAnchor, constant: 5).isActive = true
        self.radioStackView.addArrangedSubview(firstRadioButton)
        }


    let groupButtons = DLRadioButton()
    groupButtons.otherButtons = buttons
    })



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

` 输出:按钮没有直接堆叠在一起;尝试使用界面生成器但仍然无法删除间距

要在 stackView 中堆叠视图,您需要使用 addArrangedSubview

self.radioStackView.addArrangedSubview(firstRadioButton)