Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to move the done button to the right in swift

Tags:

xcode

swift

So i want to move my done button to the right of toolbar.

here is my code:-

i appreciate all answers

let picker=UIDatePicker()
func createDatePicker() {
   let toolbar = UIToolbar()
    toolbar.sizeToFit()

    // done button for toolbar
    let done = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
    toolbar.setItems([done], animated: false)

    tanggalLahirTextField.inputAccessoryView = toolbar
    tanggalLahirTextField.inputView = picker
    picker.datePickerMode = .date
}
like image 969
Indra Sen Avatar asked Dec 22 '25 10:12

Indra Sen


1 Answers

Add flexible space on left side, so your done button will automatically move to right side. Update your code as follows:

let done = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))

// Add this line of code.   
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)

toolbar.setItems([flexibleSpace, done], animated: false)
like image 78
Sagar Chauhan Avatar answered Dec 24 '25 00:12

Sagar Chauhan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!