你如何向 UIPickerView 添加按钮?

时间:2021-01-14 19:52:33

标签: ios swift uibutton uipickerview

如何向 UIPickerView 添加单个按钮,用于关闭或隐藏 PickerView?

我找到了一些解决这些问题的方法,但很多似乎都没有想出我想要的答案。 This 问题是我能找到的最接近我要问的问题,但它已经过时了,所以我想展示我的解决方案。我有一个 UIPickerView 的子类,我想添加一个 UIButton 以便能够关闭它。我想要一个带有 UIToolBar 的 UIPickerView。

下图准确描述了我正在寻找的完成按钮添加到我的 UIPickerView 子类的位置

enter image description here

您需要做的只是向 UIPickerView 添加一个 UIButton 并添加一个目标来调用某个方法,这似乎微不足道,因为我还希望 PickerView 响应用户对行的选择,按下 Done 按钮导致没有反应

1 个答案:

答案 0 :(得分:1)

创建 UIView 的子类

class CustomViewWithPicker: UIView {
    
    let picker = UIPickerView(frame: .zero)
    let pickerTitle = UILabel(frame: .zero)
    let button = UIButton(frame: .zero)
    
    let title: String = "Picker Title"
    let buttonName: String = "Button"
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        didLoad()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        didLoad()
    }
    
    func didLoad() {
        
        self.addSubview(picker)
        self.addSubview(pickerTitle)
        self.addSubview(button)
        
        picker.backgroundColor = .tertiarySystemBackground
        picker.layer.cornerRadius = 20
        picker.frame = .zero
        
        pickerTitle.text = title
        pickerTitle.font = .boldSystemFont(ofSize: 22)
        pickerTitle.textAlignment = .center
        pickerTitle.backgroundColor = .tertiarySystemBackground
        
        button.setTitle(buttonName, for: .normal)
        button.contentHorizontalAlignment = .right
        button.contentVerticalAlignment = .top
        button.isSelected = true
        
        self.updateConstraints()
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if self.point(inside: point, with: event) {
            return super.hitTest(point, with: event)
        }
        guard isUserInteractionEnabled, !isHidden, alpha > 0 else {
            return nil
        }
        
        for subview in subviews.reversed() {
            let convertedPoint = subview.convert(point, from: self)
            if let hitView = subview.hitTest(convertedPoint, with: event) {
                return hitView
            }
        }
        return nil
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    override func updateConstraints() {
        // Make Constraints ...
    }
}

在ViewController中符合UIPickerViewDelegate和UIPickerViewDataSource

class MyViewController : UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UIGestureRecognizerDelegate {
    
    let customView = CustomViewWithPicker()
    let labels = ["label0", "label1", "label2", "label3", "label4", "label5"]
    var selectedRow = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        
        customView.picker.delegate = self
        customView.picker.dataSource = self

        customView.button.addTarget(self, action: #selector(doneButtonTapped(_:)), for: .touchUpInside)
        
        self.view.addSubview(customView)
        
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return labels.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return labels[row]
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        selectedRow = row
    }
    
    @objc func doneButtonTapped(_ selectedButton: UIButton) {
        if selectedButton.isSelected {
            print("Done Button Tapped")
        }
    }
}

我四处寻找 UIPickerView 实现,它不依赖 UIToolBar 来检测按钮上的点击,但无济于事。

感谢 Duncan C. 的意见和建议