saravanar
saravanar

Reputation: 649

UIDatepicker toolbar not working in landscape mode in ios swift

Application working on both landscape and portrait. Using UIDatepicker to pick the date. Here UIDatepicker code.

class ViewController: UIViewController {
    //Text Field Connection
    @IBOutlet weak var txtDatePicker: UITextField!
    //Uidate picker
    let datePicker = UIDatePicker()

    override func viewDidLoad() {
        super.viewDidLoad()
        //show date picker
        showDatePicker()
    }
    func showDatePicker(){
        //Formate Date
        datePicker.datePickerMode = .date

        //ToolBar
        let toolbar = UIToolbar();
        toolbar.sizeToFit()

        //done button & cancel button
        let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.bordered, target: self, action: "donedatePicker")
        let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.bordered, target: self, action: "cancelDatePicker")
        toolbar.setItems([doneButton,spaceButton,cancelButton], animated: false)

        // add toolbar to textField
        txtDatePicker.inputAccessoryView = toolbar
        // add datepicker to textField
        txtDatePicker.inputView = datePicker

    }
    func donedatePicker(){
        //For date formate
        let formatter = DateFormatter()
        formatter.dateFormat = "dd/MM/yyyy"
        txtDatePicker.text = formatter.string(from: datePicker.date)
        //dismiss date picker dialog
        self.view.endEditing(true)
    }
    func cancelDatePicker(){
        //cancel button dismiss datepicker dialog
        self.view.endEditing(true)
    }
}

UIDatePicker toolbar working fine in portrait mode. enter image description here

change the app in landscape mode the toolbar get hide.

enter image description here

How to fix this issue help.

Google it and refer some link:

Add a Done button within a pop-up datePickerView in Swift?

It will not workout to me. Help me to fix this issue.

Upvotes: 1

Views: 1006

Answers (2)

Sateesh Yemireddi
Sateesh Yemireddi

Reputation: 4409

That seems like a bug in iOS 11 and which is not appearing in iOS 12. Please refer this in apple developer forums.

https://forums.developer.apple.com/thread/81650

Fix:

func textFieldDidBeginEditing(_ textField: UITextField) {

    let item = textField.inputAssistantItem
    item.leadingBarButtonGroups = []
    item.trailingBarButtonGroups = []

    //Your code here....
}

Also please have a look at this link once

Custom Keyboard InputAccessoryView not visible in iOS 11

Upvotes: 0

kalpa
kalpa

Reputation: 982

I think you need to set the frame for datepicker.

self.datePicker = UIDatePicker(frame:CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 216))

Upvotes: 0

Related Questions