2017-05-25 106 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