In many applications, such as the iphone's own Safari browser, when we click the form input box, the top right side of the software disk will have a complete button, click the Completion button to close the current soft keyboard, when the form page content is longer, the soft keyboard covers the form submission button, Providing an operation that hides the current form becomes quite useful, how does this function work, as shown in the following figure:
Ps://dailyios.oss-cn-shenzhen.aliyuncs.com/6e14b7dc797a9e440c1d.gif "/>
In fact, this feature is quite simple to implement, using the built-in Uitoolbar control, we can then achieve a similar effect for the Uitextfield or Uitextview control, we may wish to take Uitextfield as an example:
1. Add a Uitextfield control to the storyboard, as shown in the following figure:
Then set a @iboutlet member property for this control:
@IBOutlet weak var textfield:uitextfield!
2. Add a Uitoolbar child control for this textfield:
Let toolbar = Uitoolbar ()
To fit the toolbar size to the keyboard size
Toolbar.sizetofit ()
Toolbar.tintcolor = Self.view.tintColor
Bind an event function to turn off the keyboard for this button
Let do = Uibarbuttonitem.init (barbuttonsystemitem:. Done, target:self, action: "Keyboardoff");
To make the button realistic, populate a flexble placeholder button
Let Flexblebutton = Uibarbuttonitem.init (barbuttonsystemitem:. Flexiblespace, Target:nil, Action:nil);
Toolbar.items = [Flexblebutton, done];
Self.textField.inputAccessoryView = toolbar;
3. Complete the Click event:
We need to turn off the current software when we click on the button, then we need to implement the Keyboardoff function added in the second step, and the function will correspond when the button is clicked:
/**
button click Callback
*/
Func Keyboardoff () {
Self.textField.resignFirstResponder ()
}