无法在UIStackView中设置程序化布局约束 [英] Cannot Set Programmatic Layout Constraints in UIStackView

查看:240
本文介绍了无法在UIStackView中设置程序化布局约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将通过情节提要创建的UIStackView中的单选按钮居中。但是,单选按钮是根据Firebase中的数据以编程方式在UIStackView中创建的。



我正在使用



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

 `
pollRef.observe(FIRDataEventType.value,with:{(
中的快照)self.numberOfChildren = Int(snapshot.childSnapshot(forPath: answers)。childrenCount)
self .passLabel.text =字符串(self.numberOfChildren)
打印(self.numberOfChildren)

var按钮= [DLRadioButton]()

对于x in 0。 。< self.numberOfChildren {
让answerLabel = snapshot.childSnaps hot(forPath: answers)。childSnapshot(forPath:String(x + 1))。childSnapshot(forPath: answer)。value
让firstRadioButton = self.createRadioButton(frame:CGRect(x:1, y:1,宽度:80.0,高度:1.0),标题:answerLabel as!字符串,颜色:UIColor.black)
firstRadioButton.tag = x
firstRadioButton.translatesAutoresizingMaskIntoConstraints = false
buttons.append(firstRadioButton)
让页边距= self.radioStackView.layoutMarginsGuide
firstRadioButton.topAnchor.constraint(equalTo:margins.topAnchor,常数:5).isActive = true
self.radioStackView.addArrangedSubview(firstRadioButton)
}


let groupButtons = DLRadioButton()
groupButtons.otherButtons =按钮
})



}

覆盖func didReceiveMemoryWarning (){
super.didReceiveMemoryWarning()
//处理所有可以重新创建的资源。
}

`
输出:按钮不直接堆叠在彼此之上;尝试与界面生成器一起使用,但仍然无法删除间距

解决方案

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

  self.radioStackView.addArrangedSubview(firstRadioButton)


I am trying to center radiobuttons in UIStackView that I have created via the storyboard. However, the radiobuttons are created within the UIStackView programmatically based on data in Firebase.

I am using the library offered at DLRadioButtons.

UIStackView below Label, named radioStackView:

Code where Radio Buttons are added based on Firebase Data:

  `
    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.
}

` Output: Buttons not stacking directly on top of each other; tried playing with interface builder but still unable to remove spacing

解决方案

To stack views in a stackView you need to use addArrangedSubview

self.radioStackView.addArrangedSubview(firstRadioButton)

这篇关于无法在UIStackView中设置程序化布局约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆