我正试图通过故事板创建UIStackView中的radiobuttons。但是,radiobuttons是根据Firebase中的数据以编程方式在UIStackView中创建的。
我正在使用DLRadioButtons提供的图书馆。
下面标签的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.
}
答案 0 :(得分:1)
要在stackView
中堆叠视图,您需要使用addArrangedSubview
self.radioStackView.addArrangedSubview(firstRadioButton)