2017-05-25 110 views
0

我正在尝试通过故事板创建的UIStackView中的单选按钮。但是,基于Firebase中的数据以编程方式在UIStackView中创建单选按钮。无法在UIStackView中设置编程布局约束条件

我使用DLRadioButtons提供的库。

UIStackView以下标签,命名radioStackView:其中单选按钮基于火力地堡数据相加enter image description here

代码:

` 
    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

stackView要堆栈视图,您需要使用addArrangedSubview

self.radioStackView.addArrangedSubview(firstRadioButton) 
+0

感谢您的帮助,我已经更新了代码。我不能让按钮直接堆叠在一起。有什么想法吗? – tccpg288

+0

检查[间距](https://developer.apple.com/reference/uikit/uistackview/1616225-spacing)属性 –

+0

我使用界面生成器,我正在使用这些设置并且间距没有改变。 – tccpg288