Swift 3 使用 UINavBar 按钮显示以编程方式创建的 datePicker

Posted

技术标签:

【中文标题】Swift 3 使用 UINavBar 按钮显示以编程方式创建的 datePicker【英文标题】:Swift 3 Displaying a programmatically created datePicker using a UINavBar Button 【发布时间】:2017-11-14 10:50:00 【问题描述】:

我以编程方式创建了一个UIPickerView,我目前从UITextField 调用它。相反,我想在用户按下导航栏上以编程方式创建的按钮时调用datePicker。这是我的datePicker 代码目前的样子:

func doDatePicker()
    // DatePicker
    self.datePicker = UIDatePicker(frame:CGRect(x: 0, y: 0, width:     self.view.frame.size.width, height: 216))
    self.datePicker?.backgroundColor = UIColor.white
    self.datePicker?.datePickerMode = UIDatePickerMode.dateAndTime
    HVACText.inputView = self.datePicker

    // ToolBar
    let toolBar = UIToolbar()
    toolBar.barStyle = .default
    toolBar.isTranslucent = true
    toolBar.tintColor = UIColor(red: 92/255, green: 216/255, blue:  255/255, alpha: 1)
    toolBar.sizeToFit()

    // Adding Button ToolBar
    let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(HVACViewController.doneClick))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(HVACViewController.cancelClick))
    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true
    HVACText.inputAccessoryView = toolBar


我可以使用该按钮来调用通用 datePicker,但我希望用户可以使用“完成”和“取消”按钮。我还希望 datePicker 出现在 UITableView 的底部。我不想离开视图。

任何帮助将不胜感激。

这是按要求提供的屏幕截图。请注意,界面正在进行中,文本字段将消失。

【问题讨论】:

怎么了?请提供结果 UI 的屏幕截图。 【参考方案1】:

我想出了解决方案并在此处发布代码:

class HVACViewController:  UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UNUserNotificationCenterDelegate 

    @IBOutlet weak var HVACTable: UITableView!
    @IBOutlet weak var reminderCell: UITableViewCell!

    let dateFormatter = DateFormatter()
    let locale = NSLocale.current
    var datePicker : UIDatePicker!
    let toolBar = UIToolbar()

    override func viewDidLoad() 
        super.viewDidLoad()


        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))

        self.HVACTable.reloadData()
        //doDatePicker()
    

    func addTapped()
        doDatePicker()
    



    func doDatePicker()
        // DatePicker
        self.datePicker = UIDatePicker(frame:CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 200))
        self.datePicker?.backgroundColor = UIColor.white
        self.datePicker?.datePickerMode = UIDatePickerMode.dateAndTime
        datePicker.center = view.center
        view.addSubview(self.datePicker)

        // ToolBar

        toolBar.barStyle = .default
        toolBar.isTranslucent = true
        toolBar.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
        toolBar.sizeToFit()

        // Adding Button ToolBar
        let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(HVACViewController.doneClick))
        let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(HVACViewController.cancelClick))
        toolBar.setItems([cancelButton, spaceButton, doneButton], animated: true)
        toolBar.isUserInteractionEnabled = true

        self.view.addSubview(toolBar)
        self.toolBar.isHidden = false


    


    func doneClick() 
        let dateFormatter1 = DateFormatter()
        dateFormatter1.dateStyle = .medium
        dateFormatter1.timeStyle = .none
        setNotification()
        //self.datePicker.resignFirstResponder()
        datePicker.isHidden = true
        self.toolBar.isHidden = true


    

    func cancelClick() 
        datePicker.isHidden = true
        self.toolBar.isHidden = true

    

【讨论】:

以上是关于Swift 3 使用 UINavBar 按钮显示以编程方式创建的 datePicker的主要内容,如果未能解决你的问题,请参考以下文章

单击 UIButton 以显示 UIPickerView 并在按钮标题上选择 ID:Swift

Swift 5. 如何在按钮触摸时呈现或显示 ViewController?没有情节提要(以编程方式)

如何使用swift 3为按钮设置动画以淡出然后淡入

Swift,iOS - 单击以显示文本字段时按钮向上移动?然后搬回去?

UIToolBar 栏按钮项目未使用 Swift 显示

使用 Swift 以编程方式插入按钮并加载第二个视图