Published: 05 Sept 2022 › Updated: 05 Sept 2022
TechBlog - iOS - Swift - Show Keyboard with Done button
In this tech-blog, I'll explain how to show a keyboard with done button.
Step 1.
Put following class in your project.
It will work with both TextField & TextView
class KeyboardDoneToolBar: UIView {
var textField: UITextField? = nil
var textView: UITextView? = nil
func getToolbar(_ width: Double) -> UIToolbar {
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: width, height: 44))
let done = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(hideKeyboard))
let space = UIBarButtonItem(systemItem: .flexibleSpace)
toolbar.setItems([space, done], animated: false)
return toolbar
}
func setupWith(_ textField: UITextField) {
textField.inputAccessoryView = getToolbar(UIScreen.main.bounds.width)
self.textField = textField
}
func setupWithTextView(_ textView: UITextView) {
textView.inputAccessoryView = getToolbar(UIScreen.main.bounds.width)
self.textView = textView
}
@objc func hideKeyboard() {
textField?.resignFirstResponder()
textView?.resignFirstResponder()
}
}
Step 2.
Wire it with your input field
let toolbar = KeyboardDoneToolBar()
toolbar.setupWith(textField)
You're all set.
Here is how it's gonna look like.
Cheers
Have a good one
Leave TechBlog - iOS - Swift - Show Keyboard with Done button to:
Read more #hive-181436 posts
Best Posts From Sagar Kothari
More Posts From Sagar Kothari
- HiveSuite Dev Update: Unified Wallet Tokens, New Welcome Experience & Full Blockchain Explorer
- HiveSuite Dev Update: Rich Activity Insights, Infinite Scroll Improvements & Sidebar Redesign.
- HiveSuite Dev Update: Governance Enhancements, Global Snaps Creation & Platform-Wide New Tab Support.
- HiveReactKit Dev Update: Enhanced Nav, Media Playback & Governance Improvements
- HiveSuite Game Update: Spider Solitaire Added to the Games Collection
- HiveSuite Dev Update: Klondike Solitaire Joins the Games Collection.
- HiveSuite Dev Update: Faster Navigation, Expanded Profile Drawer & Bookmark Experience Improvement.
- HiveSuite Dev Update: Shareable Profile Tabs & Redesigned Profile Drawer Experience
- HiveSuite Dev Update: Smart Mentions, Internal Navigation & Seamless Inline Media Experience.
- HiveReactKit Dev Update: Inline Media Playback, Enhanced Embeds & Cleaner Post Experience.