如果我将它作为子视图添加到我的 UIPickerView,我是否需要向我的 UIToolBar 添加约束?

Do I need to add constraints to my UIToolBar if I'm adding it as a subview to my UIPickerView?

我正在尝试以编程方式向我的 UIPickerView 添加一个简单的 "Done" 按钮,而不使用故事板。

当我尝试将 toolBar 添加为 UIPickerView 的子视图时,工具栏甚至没有显示,我遇到了一些与约束相关的错误。

知道如何将按钮添加到 PickerView 吗?

这是我的代码片段:

var timerImage = UIButton()
var timer = Timer()
var timerDisplayed = 0
let image1 = UIImage(named: "stopwatch")
let timePicker = UIPickerView()
let timeSelect : [String] = ["300","240","180","120","90","60","45","30","15"]

let toolBar = UIToolbar()
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ThirdViewController.dismissKeyboard))

func pickerViewConstraints(){
    timePicker.anchor(top: nil, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.safeAreaLayoutGuide.trailingAnchor)
}

@objc func timeClock(){
    toolBar.setItems([doneButton], animated: true)
    toolBar.sizeToFit()
    toolBar.isTranslucent = false
    toolBar.isUserInteractionEnabled = true
    toolBar.barStyle = .default

    view.addSubview(timePicker)
    timePicker.addSubview(toolBar)
    pickerViewConstraints()
    timePicker.backgroundColor = .white

    DispatchQueue.main.async {
        self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.Action), userInfo: nil, repeats: true)
        self.timerImage.setImage(nil, for: .normal)
    }
}

您需要为选择器视图设置一个 UIPickerViewDelegate 类型的委托。在这个委托中,实现委托方法pickerView:viewForRow:forComponent:reusingView

为选择器项目提供自定义视图。

对于您提供的自定义视图,您可以根据需要进行设计,包括添加按钮。

看来这只是一些小问题。这是我之前的做法:

    override func viewDidLoad() {
    super.viewDidLoad()

    createToolbar()
}
    // Toolbar for "done"
    func createToolbar() {
        let toolBar = UIToolbar()
        toolBar.sizeToFit()


        let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(PageOneViewController.dismissKeyboard))

        toolBar.setItems([doneButton], animated: false)
        toolBar.isUserInteractionEnabled = true

        // Makes toolbar apply to text fields
        educationText.inputAccessoryView = toolBar
        politicalText.inputAccessoryView = toolBar
        drinkingText.inputAccessoryView = toolBar
        heightText.inputAccessoryView = toolBar
        schoolInput.inputAccessoryView = toolBar
    }

@objc func dismissKeyboard() {
        view.endEditing(true)
    }

从技术上讲,您似乎没有 "called" 工具栏/完成按钮,除非我没有看到那部分代码。此外,我还在函数中嵌套了 "done" 代码。

如果您使用的是文本字段,那么您应该能够按照 "educationText.inputAccessoryView = toolBar" 格式将上述所有代码应用于每个文本字段(教育、政治、饮酒等)。我希望这有帮助!祝你好运。

不,您不需要添加约束。它可以像:

let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(title: buttonTitle, style: .done, target: self, action: #selector(doneButtonAction))
let cancelButton = UIBarButtonItem(title: cancelTitle, style: .plain, target: self, action: #selector(cancelButtonAction))
let barAccessory = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 44))
barAccessory.barStyle = .default
barAccessory.isTranslucent = true
barAccessory.barTintColor = .blue
barAccessory.setItems([cancelButton, space, doneButton], animated: false)
picker.addSubview(barAccessory)
self.view.bringSubviewToFront(picker)

希望对您有所帮助...