Swift's keyboard type and common keyboard hiding methods

Source: Internet
Author: User

//

Viewcontroller.swift

Uitextfield Keyboard Type

//

Created by Mac on 15/4/19.

Copyright (c) years Mac. All rights reserved.

//


Import UIKit


Class Viewcontroller:uiviewcontroller, uitextfielddelegate{

var text = Uitextfield ()

var pwdtext = Uitextfield ()

Override Func Viewdidload () {

Super.viewdidload ()


Default//default type for the current input method.

Case Asciicapable//displays a keyboard which can enter ASCII characters, NON-ASCII keyboards remain active

Case Numbersandpunctuation//Numbers and assorted punctuation.

Case URL//A type optimized for URL entry (shows./. com prominently).

Case Numberpad//A number pad (0-9). Suitable for PIN entry.

Case Phonepad//A phone pad (1-9, *, 0, #, with letters under the numbers).

Case Namephonepad//a type optimized for entering a person ' s name or phone number.

Case EmailAddress//A type optimized for multiple email address entry (shows space @. prominently).

Case Decimalpad//a number pad with A decimal point.

Case Twitter//A type optimized-Twitter text entry (easy access to @ #)

Case Websearch//A default keyboard type with url-oriented addition (shows space. prominently).

set a background picture

var ImageView = Uiimageview (frame:self.view.bounds)

Let image = UIImage (named: "1.png")

Imageview.image = Image

Self.view.addSubview (ImageView)

Define user name input box

var text = Uitextfield (Frame:cgrectmake (30, Uiscreen.mainscreen ()). bounds.size.width-60

Text.delegate = Self

Text.keyboardtype = Uikeyboardtype.default

Text.layer.borderWidth = 0.5

Text.layer.borderColor = Uicolor.lightgraycolor (). Cgcolor

Text.placeholder = " Please enter user name "

Text.clearbuttonmode = Uitextfieldviewmode.always

Self.view.addSubview (text)

Text.returnkeytype = Uireturnkeytype.done

Self.text = text

Let lab = UILabel (frame:cgrectmake (0, 0, text.bounds.size.height))

Lab.text = " user name:"

Text.leftview = Lab

Lab.font = uifont.systemfontofsize (17.0)

Text.leftviewmode = Uitextfieldviewmode.always

var pwdtext = Uitextfield (Frame:cgrectmake, Uiscreen.mainscreen (). bounds.size.width-60, 30))

Pwdtext.delegate = Self

Pwdtext.keyboardtype = Uikeyboardtype.numberpad

Pwdtext.layer.borderWidth = 0.5

Pwdtext.layer.borderColor = Uicolor.lightgraycolor (). Cgcolor

Pwdtext.placeholder = " Please enter text "

Pwdtext.clearbuttonmode = uitextfieldviewmode.whileediting

Self.view.addSubview (Pwdtext)

Pwdtext.returnkeytype = Uireturnkeytype.done

Self.pwdtext = Pwdtext

let pwd = UILabel (frame:cgrectmake (0, 0, text.bounds.size.height))

Pwd.text = " Verification Code:"

Pwdtext.leftview = pwd

Pwd.font = uifont.systemfontofsize (17.0)

Pwdtext.leftviewmode = Uitextfieldviewmode.always

Do any additional seuitup after loading the view, typically from a nib.

}


// The first is to follow the proxy, is to implement the keyboard hiding in the proxy method

Override Func didreceivememorywarning () {

Super.didreceivememorywarning ()

Dispose of any resources the can be recreated.

}

// the first method. Using a proxy to hide the keyboard

Func Textfieldshouldreturn (Textfield:uitextfield), Bool {

if (TextField = = Self.text) {

Textfield.resignfirstresponder ()

Self.view.becomeFirstResponder ()

}

return true;

}

/// second method to realize keyboard hiding

Override Func Touchesbegan (Touches:nsset, withevent event:uievent) {

Self.text.resignFirstResponder ()

Self.pwdtext.resignFirstResponder ()

}

}

Swift's keyboard type and common keyboard hiding methods

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.