快速的UipickerView高度更改不起作用

时间:2018-08-17 18:08:47

标签: swift uipickerview

我知道这个问题已经被问过多次了,但是我遵循了堆栈溢出中的大多数链接,但是似乎没有答案。我还看到某个地方只允许3个高度也超过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)

enter image description here

我也尝试过此链接的答案,但没有成功How to change UIPickerView height

1 个答案:

答案 0 :(得分:2)

您需要将stackView的对齐方式更改为.center。然后,只要满足stackView条件,就可以根据需要添加witdth和height锚。

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    lazy var classLabel: UILabel = {
        let label = UILabel(frame: .zero)
        label.text = "Select Class"
        return label
    }()

    lazy var classPicker: UIPickerView = {
        let pickerView = UIPickerView(frame: .zero)
        pickerView.delegate = self
        pickerView.dataSource = self
        return pickerView
    }()

    lazy var chooseClass: UIStackView = {
        let stackView: UIStackView = UIStackView(frame: .zero)
        stackView.axis = .horizontal
        stackView.distribution = .fillEqually
        stackView.alignment = .center
        stackView.spacing = 10.0
        return stackView
    }()

    var dataSource = [1,2,3,4]

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white

        view.addSubview(chooseClass)
        chooseClass.translatesAutoresizingMaskIntoConstraints = false
        chooseClass.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        chooseClass.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        chooseClass.heightAnchor.constraint(equalToConstant: 50).isActive = true
        chooseClass.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true

        classPicker.heightAnchor.constraint(equalToConstant: 30).isActive = true
        chooseClass.addArrangedSubview(classLabel)
        chooseClass.addArrangedSubview(classPicker)
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return dataSource.count
    }

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let label = UILabel()
        label.text = String(dataSource[row])
        label.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        return label
    }

}

我使用了viewForRow委托,因为在高度30处,使用titleForRow的标题在顶部和底部被裁剪。您应该注意对齐问题,等等。这是您要执行的操作-将pickerView的高度更改为30,同时保持stackView的高度。