web-dev-qa-db-fra.com

Ajouter un bouton Terminé dans une date pop-upPickerView dans Swift?

Je veux ajouter un bouton Terminé dans une datePickerView surgie dans Swift.

Voici le code:

@IBOutlet var datePicker: UITextField!

@IBAction func dateTextInputPressed(sender: UITextField) {

    var datePickerView = UIDatePicker()
    datePickerView.datePickerMode = UIDatePickerMode.Date

    sender.inputView = datePickerView
    datePickerView.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)
}

func handleDatePicker(sender: UIDatePicker) {
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    datePicker.text = dateFormatter.stringFromDate(sender.date)
}

Je peux utiliser ce code pour afficher une datePickerView avec succès.

result of datePickerView

Mais après avoir sélectionné la date, il n'y a pas de bouton "Terminé" pour la rejeter.

Alors, comment puis-je y ajouter le bouton Terminé?

Merci!

16
He Yifei 何一非

J'ai fait cette extension pour fermer le sélecteur. Swift 2

extension UIToolbar {

 func ToolbarPiker(mySelect : Selector) -> UIToolbar {

    let toolBar = UIToolbar()

    toolBar.barStyle = UIBarStyle.Default
    toolBar.translucent = true
    toolBar.tintColor = UIColor.blackColor()
    toolBar.sizeToFit()

    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: mySelect)
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)

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

    return toolBar
}

}

Swift 3-4

extension UIToolbar {

func ToolbarPiker(mySelect : Selector) -> UIToolbar {

    let toolBar = UIToolbar()

    toolBar.barStyle = UIBarStyle.default
    toolBar.isTranslucent = true
    toolBar.tintColor = UIColor.black
    toolBar.sizeToFit()

    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: mySelect)
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)

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

    return toolBar
}

}

Et vous pouvez l'utiliser simplement dans votre viewController:

  override func viewDidLoad() {
    super.viewDidLoad()

 //Swift2
 /*
let toolBar = UIToolbar().ToolbarPiker(#selector(MyViewController.dismissPicker))

 */
// Swift3 - 4
    let toolBar = UIToolbar().ToolbarPiker(mySelect: #selector(MyViewController.dismissPicker))

    myTextField.inputAccessoryView = toolBar


}

 // Swift 2 - 3
 func dismissPicker() { 

    view.endEditing(true)

}

 // Swift 4
 @objc func dismissPicker() { 

    view.endEditing(true)

}
31
Rob

Vous pouvez utiliser un UIView générique pour la propriété inputView de la UITextField. Nous pouvons ajouter les UIDatePicker et UIButton à ce UIView.

@IBAction func dateTextInputPressed(sender: UITextField) {

    //Create the view
    let inputView = UIView(frame: CGRectMake(0, 0, self.view.frame.width, 240))


    var datePickerView  : UIDatePicker = UIDatePicker(frame: CGRectMake(0, 40, 0, 0))
    datePickerView.datePickerMode = UIDatePickerMode.Date
    inputView.addSubview(datePickerView) // add date picker to UIView

    let doneButton = UIButton(frame: CGRectMake((self.view.frame.size.width/2) - (100/2), 0, 100, 50))
    doneButton.setTitle("Done", forState: UIControlState.Normal)
    doneButton.setTitle("Done", forState: UIControlState.Highlighted)
    doneButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    doneButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Highlighted)

    inputView.addSubview(doneButton) // add Button to UIView

    doneButton.addTarget(self, action: "doneButton:", forControlEvents: UIControlEvents.TouchUpInside) // set button click event

    sender.inputView = inputView
    datePickerView.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)

    handleDatePicker(datePickerView) // Set the date on start.
}

func handleDatePicker(sender: UIDatePicker) {
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    datePicker.text = dateFormatter.stringFromDate(sender.date)
}

func doneButton(sender:UIButton)
{
    datePicker.resignFirstResponder() // To resign the inputView on clicking done.
}
14
rakeshbs

si vous utilisez un bouton au lieu d'un champ de texte:

/ ** * MARK - Action de réservation anticipée * /

@IBAction func advanceBookingAction(sender: UIButton) {

    var datePicker : UIDatePicker = UIDatePicker()
    var datePickerContainer = UIView()

    datePickerContainer.frame = CGRectMake(0.0, self.view.frame.height/2, 320.0, 300.0)
    datePickerContainer.backgroundColor = UIColor.whiteColor()

    var pickerSize : CGSize = datePicker.sizeThatFits(CGSizeZero)
    datePicker.frame = CGRectMake(0.0, 20, pickerSize.width, 460)
    datePicker.setDate(NSDate(), animated: true)
    datePicker.maximumDate = NSDate()
    datePicker.datePickerMode = UIDatePickerMode.DateAndTime
    datePicker.addTarget(self, action: "dateChangedInDate:", forControlEvents: UIControlEvents.ValueChanged)
    datePickerContainer.addSubview(datePicker)

    var doneButton = UIButton()
    doneButton.setTitle("Done", forState: UIControlState.Normal)
    doneButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
    doneButton.addTarget(self, action: Selector("dismissPicker:"), forControlEvents: UIControlEvents.TouchUpInside)
    doneButton.frame    = CGRectMake(250.0, 5.0, 70.0, 37.0)

    datePickerContainer.addSubview(doneButton)

    self.view.addSubview(datePickerContainer)

}// end advanceBookingAction

/**
* MARK - observer to get the change in date
*/

func dateChangedInDate(sender:UIDatePicker){

    var dateFormatter = NSDateFormatter()
    dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle
    dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
    objMethodInc.printvj("date selected \(datePicker.date)")

}// end dateChangedInDate

/*
* MARK - dismiss the date picker value
*/
func dismissPicker(sender: UIButton) {
    objMethodInc.printvj("dismiss date picker")
    datePickerContainer.removeFromSuperview()
}// end dismissPicker
6
Vinod Joshi

Dans Swift 5

Si vous voulez seulement Terminé bouton sur DatePicker utilisez mon code

//Write toolbar code for done button
let toolBar = UIToolbar()
toolBar.barStyle = .default
toolBar.isTranslucent = true
let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(onClickDoneButton))
toolBar.setItems([space, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
toolBar.sizeToFit()
dobTF.inputAccessoryView = toolBar //Change your TextField name here

@objc func onClickDoneButton() {
    self.view.endEditing(true)
}
0
iOS