gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:37:14 25 4
gpt4 key购买 nike

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

我正在使用 DLRadioButtons 提供的库.

Label下面的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

最佳答案

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

self.radioStackView.addArrangedSubview(firstRadioButton)

关于ios - 无法在 UIStackView 中设置编程布局约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44171066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com