键盘文本字段是否取决于文本字段约束?

时间:2018-01-05 11:21:52

标签: swift textfield

我遇到键盘文字问题。我正在使用iOS 9.2中的swift 4

我有一个登录屏幕,它有很多限制(适用于每个屏幕)。在我的登录屏幕中,我有一个用户名和密码文本字段。

问题是当我在我的IPad上启动应用程序时,键盘出现在相关textField的中心。此外,观察者将无法工作:当我使用调试器时,我从未进入观察者

但是当我在模拟器上启动时,一切都很完美。

#My object life cycle 
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    NotificationCenter.default.addObserver(self, selector:  #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}


# My textfield delegates and observers
extension LoginViewController {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}


@objc func keyboardWillShow(_ notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == 0{
            self.view.frame.origin.y -= keyboardSize.height
        }
    }
}

@objc func keyboardWillHide(_ notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y != 0{
            self.view.frame.origin.y += keyboardSize.height
        }
    }
}

}

对于我的textfield storyboard配置,这是它

Look textField control textfield constraint

那么我没理解什么?

  • 键盘是否也依赖于文本字段限制?
  • 为什么观察者不在iPad上工作但是在模拟中工作?
  • 如何以编程方式移动键盘?

3 个答案:

答案 0 :(得分:0)

确保您的iPad / iPhone有键盘底座..

https://discussions.apple.com/thread/5430957

答案 1 :(得分:0)

您不是在扩展程序中继承UITextFieldDelegate。另外,请确保在viewDidLoad方法中将文本字段设置为自己委托。

@IBOutlet weak var yourViewName: UIView!

override func viewDidLoad() {

   loginTextField.delegate = self
   passwordTextField.delegate = self

   super.viewDidLoad()

   NotificationCenter.default.addObserver(self, selector:#selector(LoginViewController.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)

   NotificationCenter.default.addObserver(self, selector:#selector(LoginViewController.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)

}

extension LoginViewController : UITextFieldDelegate {

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

    @objc func keyboardWillShow(notification: NSNotification) {
        UIView.animate(withDuration: 0.3) {
            self.usernameField.frame.origin.y -= 43
            self.usernameLine.frame.origin.y -= 43
            self.passwordField.frame.origin.y -= 43
            self.passwordLine.frame.origin.y -= 43
            self.loginBtn.frame.origin.y -= 65
        }
    }

    @objc func keyboardWillHide(notification: NSNotification) {
        UIView.animate(withDuration: 0.3) {
            self.usernameField.frame.origin.y += 33
            self.usernameLine.frame.origin.y += 33
            self.passwordField.frame.origin.y += 33
            self.passwordLine.frame.origin.y += 33
            self.loginBtn.frame.origin.y += 55
        }
    }

答案 2 :(得分:0)

简便的方法,首先获取一个滚动视图,然后将top,bottom,Leading,railing包含所有4个值均为0的内容,然后采用一个视图并为view的top,bottom,leading,traling添加4个约束将您的文本字段放入该视图中,并根据您的设计概念添加相对于您在此scrollview中添加的视图的约束,

现在,只要您打开键盘,视图就会朝上,文本字段和键盘都将正确显示