我知道这个问题已经被问过多次了,但我已经按照stackoverflow上的大部分链接尝试了,但没有一个答案有效。还有一些地方说只允许使用三种高度,而且都在160以上,而这并不适用于我的情况。因此,请提供一种解决方法。
这是我尝试过的代码。
这是我尝试过的代码。
//this label doesn't matter
let classLabel: UILabel = {
let label = UILabel()
label.text = "Select Class"
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
//first try
var classPicker = UIPickerView(frame: CGRect(x: 0.0, y: 0.0, width:
30.0, height: 30.0))
//second try
var classPicker: UIPickerView = {
let picker = UIPickerView(frame: CGRect(x: 0.0, y: 0.0, width: 10.0, height: 10.0))
picker.translatesAutoresizingMaskIntoConstraints = false
return picker
}()
//third
lazy var chooseClass: UIStackView = {
classPicker.clipsToBounds = true
classPicker.heightAnchor.constraint(equalToConstant: 30.0)
let stackView: UIStackView = UIStackView(arrangedSubviews: [self.classLabel, self.classPicker])
stackView.axis = .horizontal
stackView.distribution = .fillEqually
stackView.alignment = .fill
stackView.spacing = 10.0
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()
//and then
chooseClass.heightAnchor.constraint(equalToConstant: 30.0)
我也尝试了这个链接中的答案,但没有成功 如何更改UIPickerView的高度