0

我正在尝试以UIPickerView编程方式向我的添加一个简单的“完成”按钮,而不使用情节提要。

当我尝试添加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)
    }
}
4

3 回答 3

1

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

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)

希望能帮助到你...

于 2020-01-21T06:25:53.430 回答
0

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

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

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

于 2020-01-21T03:23:09.003 回答
0

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

    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)
    }

似乎您在技术上没有“调用”工具栏/完成按钮,除非我只是没有看到那部分代码。我还在函数中嵌套了“完成”代码。

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

于 2020-01-21T03:23:28.073 回答