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

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

Swift crifan 4203浏览 0评论

swift中,textField中:

       //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
参考:
用代码:
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.191 seconds, using 22.07MB memory