1
我已經創建了一個自定義UIView
並且在視圖內部,我嘗試使用UIStackView
添加幾個UI元素。以編程方式使用堆棧視圖
IB內部我添加了一個視圖,然後將自定義類設置爲我的自定義UIView。
該UIStackView
似乎不被約束到UIView
我添加它,而不是UIStackView
似乎錨定到原點。另外,我只能在調試視圖層次結構中看到它。它實際上並沒有顯示任何標籤或按鈕,我只看到一個灰色的虛線框。
這裏是我使用不當UIStackView
我創建
import UIKit
class ParameterControl: UIView {
private var name : UILabel!
private var valTf : UITextField!
private var delta : UITextField!
private var stepper : UIStepper!
private var button : UIButton!
init(frame: CGRect, albumCover: String) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit(){
name = UILabel()
name.text = "Name"
name.backgroundColor = UIColor.blue
valTf = UITextField.init()
delta = UITextField.init()
stepper = UIStepper.init()
button = UIButton.init()
let stackView = UIStackView(arrangedSubviews: [valTf, delta, stepper, button])
stackView.axis = .horizontal
stackView.distribution = .fillEqually
stackView.alignment = .fill
stackView.spacing = 10
stackView.translatesAutoresizingMaskIntoConstraints = false
addSubview(stackView)
//let viewsDictionary = ["stackView":stackView]
//let stackView_H = NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[stackView]-20-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
//let stackView_V = NSLayoutConstraint.constraints(withVisualFormat: "V:|-30-[stackView]-30-|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: viewsDictionary)
//self.addConstraints(stackView_H)
//self.addConstraints(stackView_V)
}
}
自定義視圖?目標是能夠創建這些視圖的數組,每個參數都有一個。我不想在IB中單獨創建這些視圖。
您需要添加約束到您的堆棧視圖,以限制它的超級視圖(自定義類中的自我) – Paulw11