如何在swift中将完成按钮向右移动

时间:2018-04-27 07:10:49

标签: swift xcode

所以我想将完成按钮移动到工具栏右侧。

这是我的代码: -

我感谢所有答案

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
}

1 个答案:

答案 0 :(得分:1)

在左侧添加灵活空间,因此您的完成按钮将自动移动到右侧。更新您的代码如下:

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)