Swift-click the external screen of the input box to close the virtual keyboard

Source: Internet
Author: User
Tags uicontrol

If we set the return key of the text box to done, then we associate the Do End on Exit event of the text box in the code with storyboard. The Resignfirstresponder () method of the text box is called in the associated code. You can enable the user to turn off the virtual keyboard when they click on the keyboard return key.
You can also turn off the virtual keyboard if you want to implement a stage screen other than when the user taps the text box. This is where the background of the screen responds to the touch down event.  (1) Because the background is a UIView control, you cannot drag the associated touch event directly. The first step is to make the background a Uicontrol control. Click the Show the Identity Inspector icon in the upper-right corner and set the custom class class to Uicontrol to associate with the event.
(2) Drag the background into the code to associate the touch up inside event
(3) In the background click event, turn off the virtual keyboard
123456789101112131415161718 import UIKitclass ViewController: UIViewController {    @IBOutlet weak var tf: UITextField!        override func viewDidLoad() {        super.viewDidLoad()    }    @IBAction func viewClick(sender: AnyObject) {        tf.resignFirstResponder()    }         override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()    }}

Swift-click the external screen of the input box to close the virtual keyboard

Related Article

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.