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

时间:2017-06-12 20:34:15

标签: ios swift datepicker

我已经以编程方式创建了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的底部。我不想偏离观点。

任何帮助都将不胜感激。

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

Screen Shot

1 个答案:

答案 0 :(得分:5)

我找到了解决方案并在此处发布了代码:

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

    }