Created
February 11, 2019 16:01
-
-
Save zwaldowski/e99925f3e0d366ea55b24108400c65c4 to your computer and use it in GitHub Desktop.
iOS Done Accessory Bar — The two versions below are mutually exclusive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class DoneAccessoryView: UIInputView { | |
private let toolbar = UIToolbar() | |
init() { | |
super.init(frame: .zero, inputViewStyle: .default) | |
translatesAutoresizingMaskIntoConstraints = false | |
allowsSelfSizing = true | |
toolbar.translatesAutoresizingMaskIntoConstraints = false | |
toolbar.items = [ | |
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), | |
UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissKeyboard)) | |
] | |
addSubview(toolbar) | |
NSLayoutConstraint.activate([ | |
toolbar.topAnchor.constraint(equalTo: topAnchor), | |
toolbar.leadingAnchor.constraint(equalTo: leadingAnchor), | |
bottomAnchor.constraint(equalTo: toolbar.bottomAnchor), | |
trailingAnchor.constraint(equalTo: toolbar.trailingAnchor) | |
]) | |
} | |
required init?(coder decoder: NSCoder) { | |
fatalError() | |
} | |
override func safeAreaInsetsDidChange() { | |
super.safeAreaInsetsDidChange() | |
toolbar.setNeedsLayout() | |
} | |
@objc private func dismissKeyboard(_ sender: Any) { | |
// Left as an exercise to the reader | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class DoneAccessoryViewController: UIInputViewController { | |
private var toolbar: UIToolbar! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
guard let inputView = inputView else { return } | |
inputView.translatesAutoresizingMaskIntoConstraints = false | |
inputView.allowsSelfSizing = true | |
let toolbar = UIToolbar() | |
toolbar.translatesAutoresizingMaskIntoConstraints = false | |
toolbar.items = [ | |
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), | |
UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissKeyboard)) | |
] | |
inputView.addSubview(toolbar) | |
self.toolbar = toolbar | |
NSLayoutConstraint.activate([ | |
toolbar.leadingAnchor.constraint(equalTo: inputView.leadingAnchor), | |
toolbar.topAnchor.constraint(equalTo: inputView.topAnchor), | |
inputView.safeAreaLayoutGuide.bottomAnchor.constraint(equalTo: toolbar.bottomAnchor), | |
inputView.trailingAnchor.constraint(equalTo: toolbar.trailingAnchor) | |
]) | |
} | |
override func viewSafeAreaInsetsDidChange() { | |
super.viewSafeAreaInsetsDidChange() | |
toolbar.setNeedsLayout() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment