Xcode9学习笔记27

来源:互联网 发布:软件成本估算专家 编辑:程序博客网 时间:2024/05/17 10:07

//添加文本框代理协议UITextFieldDelegate,使用协议中的方法,在完成文本框文字的输入后,隐藏系统键盘的显示class ViewController: UIViewController,UITextFieldDelegate {    override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.        let rect = CGRect(x: 60, y: 80, width: 200, height: 30)        let textField = UITextField(frame: rect)//初始化文本输入框对象,设置位置、尺寸属性                textField.borderStyle = UITextBorderStyle.roundedRect//设置文本框对象的边框样式为圆角矩形        textField.placeholder = "Your Email"//设置文本框的占位符属性        textField.autocorrectionType = UITextAutocorrectionType.no//关闭文本框对象的语法错误提示功能        textField.returnKeyType = UIReturnKeyType.done//设置在文字输入时,在键盘面板上,回车按钮的类型        textField.clearButtonMode = UITextFieldViewMode.whileEditing//设置文本框对象右侧的清除按钮,仅在编辑状态时显示        textField.keyboardType = UIKeyboardType.emailAddress//设置文本框对象的键盘类型        textField.keyboardAppearance = UIKeyboardAppearance.dark//设置文本框对象的键盘为暗色主题        textField.delegate = self//设置文本框对象的代理为当前视图控制器                self.view.addSubview(textField)//将文本框对象添加到当前视图控制器的根视图    }        //添加一个代理方法,当用户按下键盘上的回车键时,调用此方法    func textFieldShouldReturn(_ textField: UITextField) -> Bool {        textField.resignFirstResponder()//当用户按下键盘上的回车键时,文本框对象失去焦点,键盘也将自动隐藏        return true    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }}



原创粉丝点击