最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Swift中TextField中键盘点击完成后键盘不能消失

Swift crifan 4308浏览 0评论

swift中,textField中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//3. username/phone number textFiled
 let textFieldPaddingX:CGFloat = 10
 let textFieldPaddingY:CGFloat = 20
 let textFieldHeight:CGFloat = 40
 usernameTextField.frame = CGRectMake(
     textFieldPaddingX,
     appNameLabel.frame.origin.y + appNameLabel.frame.height + 40,//self.view.bounds.height*0.4,
     self.view.bounds.width - (textFieldPaddingX * 2),
     textFieldHeight)
 usernameTextField.textAlignment = NSTextAlignment.Left
 usernameTextField.font = UIFont.systemFontOfSize(16)
 usernameTextField.borderStyle = UITextBorderStyle.RoundedRect
 usernameTextField.placeholder = "请输入用户名(手机号)"
 usernameTextField.autocorrectionType = UITextAutocorrectionType.No
 usernameTextField.keyboardType = UIKeyboardType.Default
 usernameTextField.returnKeyType = UIReturnKeyType.Done
 usernameTextField.clearButtonMode = UITextFieldViewMode.WhileEditing
 usernameTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
 
 //4. password textFiled
 passwordTextField.frame = CGRectMake(
     usernameTextField.frame.origin.x,
     usernameTextField.frame.origin.y + usernameTextField.frame.height + textFieldPaddingY,
     usernameTextField.frame.width,
     usernameTextField.frame.height)
 passwordTextField.textAlignment = NSTextAlignment.Left
 passwordTextField.font = UIFont.systemFontOfSize(16)
 passwordTextField.borderStyle = UITextBorderStyle.RoundedRect
 passwordTextField.placeholder = "请输入密码"
 passwordTextField.secureTextEntry = true
 passwordTextField.autocorrectionType = UITextAutocorrectionType.No
 passwordTextField.keyboardType = UIKeyboardType.Default
 passwordTextField.returnKeyType = UIReturnKeyType.Done
 passwordTextField.clearButtonMode = UITextFieldViewMode.WhileEditing
 passwordTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
键盘可以出现,但是点击完成后,键盘不能消失:
click done but keyboard not dismiss
搜:
swift keyboard show and dismiss
参考:
用代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class LoginViewController: UIViewController, UITextFieldDelegate {
 
        //3. username/phone number textFiled
。。。
        usernameTextField.delegate = self
 
        //4. password textFiled
。。。
        passwordTextField.delegate = self
 
    /***************************************************************************
     * UITextFieldDelegate Functions
     ***************************************************************************/
 
    //called when 'return' key pressed. return NO to ignore.
    func textFieldShouldReturn(textField: UITextField) -> Bool
    {
        textField.resignFirstResponder()
        return true;
    }
点击Done后,键盘就可以消失了:
keyboard can dismiss when done

转载请注明:在路上 » [已解决]Swift中TextField中键盘点击完成后键盘不能消失

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.268 seconds, using 22.15MB memory