简体   繁体   English

当视图已经显示时如何防止键盘向上移动视图

[英]How to prevent keyboard from moving the view up when it is already shown

I got the keyboard to move up when I click a textfield but as soon as I click another textfield It move the view up even more.当我单击一个文本字段时,我让键盘向上移动,但是一旦我单击另一个文本字段,它就会将视图向上移动得更多。 Thank you for your help.感谢您的帮助。

func keyboardStates() {
        NotificationCenter.default.addObserver(self, selector: 
#selector(keyboardWillChange(notification:)), name: 
UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: 
#selector(keyboardWillChange(notification:)), name: 
UIResponder.keyboardWillHideNotification, object: nil)
}

deinit {
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}


@objc func keyboardWillChange(notification: Notification){

    guard let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {

        return
    }

    if notification.name == UIResponder.keyboardWillShowNotification {
        view.frame.origin.y = -keyboardRect.height/3
    } else {
        view.frame.origin.y = 0
    }
}

Try the below method,试试下面的方法,

@objc
    func keyboardWillChange(_ notification: NSNotification) {
        guard let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double else { return }
        guard let curve = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt else { return }
        guard let startingFrame = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue else { return }
        guard let endingFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
        let deltaY = endingFrame.origin.y - startingFrame.origin.y
        UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
            self.frame.origin.y += deltaY
        }, completion: nil)
    }
var keyboardHasShown = false 

@objc func keyboardWillChange(notification: Notification){

guard let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {

    return
}

if notification.name == UIResponder.keyboardWillShowNotification {
    if keyboardHasShown == false {
      view.frame.origin.y = -keyboardRect.height/3

   }
    keyboardHasShown = true
} else {
    view.frame.origin.y = 0
    keyboardHasShown = false
}
}

Well doing it per view controller basis will be difficult and time consuming, i would recommend to use this fantastic library for such tasks, it supports, scroll view, table view and collection view.在每个视图控制器基础上做得好将是困难和耗时的,我建议将这个出色的库用于此类任务,它支持滚动视图、表格视图和集合视图。

https://github.com/hackiftekhar/IQKeyboardManager https://github.com/hackiftekhar/IQKeyboardManager

Give it a try and i assure you will like it too.试一试,我保证你也会喜欢它。

Regards, Harish问候, 哈里斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当使用带有多个文本字段的键盘时,如何防止视图向上移动多余的空间 - How to prevent view from moving up extra space when using keyboard with multiple text fields 防止表格视图页脚与键盘一起向上移动 - Prevent the table view footer from moving up along with the keyboard 如何防止键盘启动时表格视图不选择单元格 - How to prevent table view from not selecting the cell when keyboard is up 当键盘显示自动布局时,UIView不会向上移动 - UIView is not moving up when keyboard shown AutoLayout 在iPad上,如何防止模态视图在键盘呈现时向上移动? - On iPad, how to prevent the modal view from moving up upon keyboard presenting? Swift 防止 TabBar 在键盘处于活动状态时向上移动 - Swift prevent TabBar from moving up when Keyboard is active 在显示键盘时不使用滚动视图(iOS)的情况下上下调整视图的最佳做法是什么? - What is a good practice to adjust/moving view up and down when keyboard is shown, without using a scroll view(iOS)? 如何防止的UITextView从键盘秀动起来 - How to prevent UITextView from moving up on keyboard show 键盘显示时向上推视图 - Push view up when keyboard shown 如果我从另一个已经显示键盘的视图控制器转换,如何检测当前是否显示键盘? - How do I detect if keyboard is currently shown, if I transitioned from another view controller that was already showing the keyboard?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM