简体   繁体   English

当键盘显示UITextView而不是同一视图上的UITextField时,滚动视图

[英]Scroll view when keyboard appears for UITextView, but not for the UITextField that's on the same view

I am using this code to scroll the view up when I start editing a UITextView . 当我开始编辑UITextView时,我正在使用此代码向上滚动视图。

override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(Login.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(Login.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

}

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
        }
    }

}

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
        }
    }
}

However, I also have a UITextField at the top of this view, and the view also scrolls when I begin editing it. 但是,我在该视图的顶部也有一个UITextField ,当我开始对其进行编辑时,该视图也会滚动。 I do not want this to happen. 我不希望这种情况发生。 How can I change my code to only scroll when the UITextView keyboard is active and not when the UITextField keyboard is activated? 如何将我的代码更改为仅在激活UITextView键盘时滚动而不在激活UITextField键盘时滚动?

Try this piece of code, user textview delgate 尝试这段代码,用户使用textview delgate

// scrollBy - pass the height you want your scrollview to be scrolled when keyboard appears // scrollBy-传递您希望在键盘出现时滚动滚动视图的高度

 func textViewDidBeginEditing(_ textView: UITextView)
    {
         scrView.setContentOffset(CGPoint.init(x: 0, y: scrollBy), animated: true)

     }

    func textViewDidEndEditing(_ textView: UITextView)
    {
        scrView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)

        self.view.endEditing(true);
    }

我建议您应该在项目中添加TPkeyboard File而不是编码,这会在需要时切换键盘

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

相关问题 出现键盘时向上移动UITextField / View,但仅当TextFiel隐藏时 - Move UITextField/View up when keyboard appears, but only if TextFiel is hidden 切换UITextField(Swift)时出现键盘调整视图 - Adjust View for keyboard appears when switching UITextField (Swift) 迅速出现键盘时滚动UITextView - Scroll UITextView when keyboard appears in swift 如何在键盘出现时使视图控制器滚动到文本字段 - How to make the view controller scroll to text field when keyboard appears 当键盘出现BUG时,将表格视图滚动到顶部 - Scroll table view row to top when keyboard appears BUG 创建UITextView时,如何在Live View中显示的Swift Playgrounds(在Xcode上)的UI中隐藏键盘? - How can I hide the keyboard in the UI in Swift Playgrounds (on Xcode) that appears in the Live View when I create a UITextView? iOS 7 iPad方向问题,当键盘出现在UITextField上时视图向上移动时 - iOS 7 iPad Orientation issue, when view moves upside after keyboard appears on UITextField 操作方法:当UITextView不在视图时隐藏键盘? - How To: hide keyboard when UITextView is out of view? 键盘在出现时会隐藏UITextView - Keyboard hides UITextView when it appears 键盘出现时调整UITextView的大小 - Resize the UITextView when keyboard appears
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM