To add a completion button to a form control by using Uitoolbar

Source: Internet
Author: User

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 ()
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.