【问题标题】:Raise the stackView above the keyboard将 stackView 提升到键盘上方
【发布时间】:2020-12-18 12:13:01
【问题描述】:

我有一个登录屏幕。在中间(X:0, Y:0) 有一个stackView,其中有两个文本字段。当我想在文本字段中输入内容时,会显示键盘并提升 stackView,只要我输入一个字符,它就会到位并且什么都看不到。告诉我如何确保在文本字段中输入字符时stackView不会掉下!!

  //MARK: Override
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        ///NotificationCenter keyboard
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
    }

 override func viewWillDisappear(_ animated: Bool) {
         super.viewWillDisappear(animated)
         
         NotificationCenter.default.removeObserver(self)
     }

// Extension keyboard SHOW/HIDE
extension LoginViewController {
    @objc func keyboardWillShow(notification: NSNotification) {
        var keyboardHeight = CGFloat(0.0)
        
        if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
            let keyboardRectangle = keyboardFrame.cgRectValue
            keyboardHeight = keyboardRectangle.height
        }
        
        UIView.animate(withDuration: 3) {
            self.conteinerStackView.frame.origin.y  =  keyboardHeight - 100
            self.view.layoutIfNeeded()
        }
    }
    
    @objc func keyboardWillHide() {
        self.view.layoutIfNeeded()
        UIView.animate(withDuration: 1) {
            self.conteinerStackView.frame.origin.y = 0
            self.view.layoutIfNeeded()
        }
    }
}

【问题讨论】:

  • 你能发一张图片来证明这个问题吗?

标签: ios swift iphone view stack


【解决方案1】:

尝试为 stackView 设置底部约束并在观察者函数中使用它。

@objc func keyboardWillAppear(notification: Notification)
{
       let keyboardRect: CGRect = notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! CGRect
        
        let keyboardSize: CGSize = keyboardRect.size
        
        bottomConstraint.constant = keyboardSize.height + 20
}

 @objc func keyboardWillDisAppear(notification: Notification)
{
        bottomConstraint.constant = 20
}

【讨论】:

    【解决方案2】:

    这个问题有很多解决方案。

    1. 使用IQKeyboardManager

      无代码插入式通用库允许防止键盘向上滑动和覆盖 UITextField/UITextView 的问题。既不需要编写任何代码,也不需要任何设置等等。

    使用安装或查看文档

    pod 'IQKeyboardManagerSwift' 
    
    1. 使用键盘观察器

      override func viewDidLoad() {
          super.viewDidLoad()
          NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
          NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
      }
      
      @objc func keyboardWillShow(notification: NSNotification) {
          if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
              self.view.frame.origin.y -= keyboardSize.height
          }
      }
      
      @objc func keyboardWillHide(notification: NSNotification) {
          self.view.frame.origin.y = 0
      }
      
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多