In this example I use a UIToolBar to contain the done button but you can use any UIView to assign to inputAccessoryView.
Here is an example of using a Navigation Bar:
(Xcode 8, Swift 3.0)
class ViewController: UIViewController { @IBOutlet weak var nameTextField: UITextField! @IBOutlet weak var phoneNumberTextField: UITextField! override func viewDidLoad() { super.viewDidLoad() let keyboardToolBar = UIToolbar() keyboardToolBar.sizeToFit() let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.doneClicked) ) keyboardToolBar.setItems([flexibleSpace, doneButton], animated: true) nameTextField.inputAccessoryView = keyboardToolBar phoneNumberTextField.inputAccessoryView = keyboardToolBar } func doneClicked() { view.endEditing(true) } }
Here is an example of using a Navigation Bar:
let navItem = UINavigationItem() let doneBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.doneClicked)) navItem.rightBarButtonItem = doneBtn let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 44)) navBar.pushItem(navItem, animated: true) nameTextField.inputAccessoryView = navBar phoneNumberTextField.inputAccessoryView = navBar
(Xcode 8, Swift 3.0)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.