码迷,mamicode.com
首页 > 编程语言 > 详细

Swift之键盘类型与常见的键盘隐藏方法

时间:2015-04-20 09:33:49      阅读:684      评论:0      收藏:0      [点我收藏+]

标签:uitextfield   swift教程   swift教学视频   swift   ios   

//

//  ViewController.swift

//  UITextField键盘类型

//

//  Created by mac on 15/4/19.

//  Copyright (c) 2015 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 for twitter text entry (easy access to @ #)

//        case WebSearch // A default keyboard type with URL-oriented addition (shows space . prominently).

        

        // 设置背景图片

        var imageview = UIImageView(frame: self.view.bounds)

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

        imageview.image = image

        self.view.addSubview(imageview)

        

      // 定义用户名输入框

        var text = UITextField(frame: CGRectMake(30, 100, UIScreen.mainScreen().bounds.size.width-60, 30))

        text.delegate = self

        text.keyboardType = UIKeyboardType.Default

        text.layer.borderWidth = 0.5

        text.layer.borderColor = UIColor.lightGrayColor().CGColor

        text.placeholder = "请输入用户名"

        text.clearButtonMode = UITextFieldViewMode.Always

        self.view.addSubview(text)

         text.returnKeyType = UIReturnKeyType.Done

        self.text = text

        

        let lab = UILabel(frame: CGRectMake(0, 0, 70, text.bounds.size.height))

        lab.text = "用户名:"

        text.leftView = lab

//        lab.font = UIFont.systemFontOfSize(17.0)

        text.leftViewMode = UITextFieldViewMode.Always

        

        var pwdtext = UITextField(frame: CGRectMake(30, 150, 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 = "请输入文字"

        pwdtext.clearButtonMode = UITextFieldViewMode.WhileEditing

        self.view.addSubview(pwdtext)

        pwdtext.returnKeyType = UIReturnKeyType.Done

        self.pwdtext = pwdtext

        

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

       pwd.text = "验证码:"

        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.

    }


//    第一种是遵循代理,是在代理方法中实现键盘的隐藏

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    // 第一种方法。用代理实现键盘的隐藏

    func textFieldShouldReturn(textField: UITextField) -> Bool {

        if (textField == self.text) {

           

            textField.resignFirstResponder()

//            self.view.becomeFirstResponder()

        }

            return true;

    }

    

//    第二种方法实现键盘的隐藏

      override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

        self.text.resignFirstResponder()

        self.pwdtext.resignFirstResponder()

    }

}

Swift之键盘类型与常见的键盘隐藏方法

标签:uitextfield   swift教程   swift教学视频   swift   ios   

原文地址:http://blog.csdn.net/mjbaishiyun/article/details/45132945

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!