2017-06-12 71 views
2

我創建了一個UIPickerView編程,我目前從UITextField調用。相反,當用戶按下在導航欄上以編程方式創建的按鈕時,我想調用datePicker。這就是我的datePicker代碼目前的樣子:Swift 3使用UINavBar按鈕顯示一個以編程方式創建的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出現在UITableView的底部。我不想離開這個觀點。

任何幫助,將不勝感激。

這是根據要求的屏幕截圖。請注意,界面正在進行中,文本框將會消失。

Screen Shot

+0

有什麼不對?請提供最終用戶界面的屏幕截圖。 – kelin

回答

2

我想出瞭解決方案,並在這裏張貼代碼:

類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 

} 
相關問題