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

时间:2017-05-25 01:52:38

标签: ios swift constraints uistackview

我正试图通过故事板创建UIStackView中的radiobuttons。但是,radiobuttons是根据Firebase中的数据以编程方式在UIStackView中创建的。

我正在使用DLRadioButtons提供的图书馆。

下面标签的UIStackView,名为radioStackView:enter image description here

基于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.
}

` 输出:按钮不直接堆叠在一起;尝试使用界面生成器但仍无法删除间距 enter image description here

1 个答案:

答案 0 :(得分:1)

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

self.radioStackView.addArrangedSubview(firstRadioButton)