【问题标题】:How to fix position of UIView when keyboard moved up?键盘向上移动时如何修复 UIView 的位置?
【发布时间】:2018-06-21 05:57:23
【问题描述】:

当键盘向上移动时,如何用imagelabel固定UIView的位置?

当键盘显示时,我编写了一个代码来向上移动我的textField,但这会移动所有内容。

func textFieldDidEndEditing(_ textField: UITextField) {
    moveTextField(textField: rasstoyanietextField, moveDistance: -215, up: false)
}


func textFieldDidBeginEditing(_ textField: UITextField) {
    moveTextField(textField: rasstoyanietextField, moveDistance: -215, up: true)
}


func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    rasstoyanietextField.resignFirstResponder()

    return true
}


func moveTextField(textField: UITextField, moveDistance: Int, up: Bool){
    let moveDuration = 0.1
    let movement: CGFloat = CGFloat(up ? moveDistance : -moveDistance)

    UIView.beginAnimations("animateTextField", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(moveDuration)
    self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
    UIView.commitAnimations()

}

我该如何解决?

【问题讨论】:

  • 你最好使用通知。检查this
  • 1.不要硬编码键盘高度。 2. 你从不使用moveTextField 方法的textField 参数。 3.使用块版的UIView动画。这要容易得多。

标签: ios swift uiview keyboard uitextfield


【解决方案1】:

因为您移动整个视图,而不是仅移动 textview。换行:

self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)

与:

textField.frame = textField.frame.offsetBy(dx: 0, dy: movement)

【讨论】:

    【解决方案2】:

    添加键盘观察者

      NotificationCenter.default.addObserver(
                    self,
                    selector: #selector(handleKeyboardDidShow),
                    name: NSNotification.Name.UIKeyboardDidShow,
                    object: nil)
    
    
                NotificationCenter.default.addObserver(
                    self,
                    selector: #selector(handleKeyboardWillHide),
                    name:NSNotification.Name.UIKeyboardWillHide,
                    object: nil)
    

    我认为最好在具有 textfeilds/textViews 的 viewControllers 中使用滚动视图,以便在显示时向上移动所有项目,但在这里你可以钩住 textfeild 的底部约束或其超级视图并将其拖动为 IBOutlet 并执行此操作

     @objc func handleKeyboardDidShow(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
    
              self.textFeilfBottomCon.constant = -1 * keyboardSize.height
    
              UIView.animate(withDuration: 0.5, animations: {
    
                  self.view.layoutIfNeeded()
    
             })
    
    
        }
    }
    
    @objc func handleKeyboardWillHide(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
    
                self.textFeilfBottomCon.constant = 0
    
                 UIView.animate(withDuration: 0.5, animations: {
    
                  self.view.layoutIfNeeded()
    
             })
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 2011-12-18
      • 2014-11-15
      • 2021-11-30
      相关资源
      最近更新 更多