ios - UIScrollView 在键盘关闭时停止滚动

标签 ios uiscrollview swift3

我目前正在使用 Swift 3 为 iPhone 开发一个应用程序,但是我遇到了 ScrollView 的问题。 在选择文本字段并显示键盘之前, ScrollView 正常工作(即:我可以上下滚动),但是在关闭键盘后, ScrollView 不再允许我滚动,这是我的问题。 注意:如果我再次选择一个文本字段并使键盘出现,它可以正常工作并在再次关闭后停止工作。

我检查了 ScrollView 的 isScrollEnabled 属性,它似乎已启用。不幸的是,我仍然不太熟悉 ScrollView 的所有细节,似乎无法弄清楚它停止工作的原因。

任何关于我可以在哪里寻找的帮助或指示将不胜感激。

编辑:有相当多的代码,但这里是与 ScrollView 和键盘相关的缩小部分:

class ScrollViewController: UIViewController, UITextViewDelegate, UITextFieldDelegate {
//Scroll view
    @IBOutlet weak var scrollView: UIScrollView!
    //UIView inside the scroll view
    @IBOutlet weak var contentView: UIView!
    //Save button on the top right corner
    @IBOutlet weak var saveButton: UIBarButtonItem!
    //Text field being editted
    var activeTextField:UITextField?

    fileprivate var contentInset:CGFloat?
    fileprivate var indicatorInset:CGFloat?

override func viewDidLoad() {
        contentInset = scrollView.contentInset.bottom
        indicatorInset = scrollView.scrollIndicatorInsets.bottom

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(ScrollViewController.keyboardWillShow(_:)),
                                               name: NSNotification.Name.UIKeyboardWillShow,
                                               object: nil)
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(ScrollViewController(_:)),
                                               name: NSNotification.Name.UIKeyboardWillHide,
                                               object: nil)
}

func adjustInsetForKeyboardShow(_ show: Bool, notification: Notification) {
        let userInfo = notification.userInfo ?? [:]
        let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
        let adjustmentHeight = (keyboardFrame.height + 20) * (show ? 1 : -1)

        scrollView.contentInset.bottom = (contentInset! + adjustmentHeight)
        scrollView.scrollIndicatorInsets.bottom = (indicatorInset! + adjustmentHeight)
    }

    func keyboardWillShow(_ notification: Notification) {
        adjustInsetForKeyboardShow(true, notification: notification)
    }

    func keyboardWillHide(_ notification: Notification) {
        adjustInsetForKeyboardShow(false, notification: notification)
    }

    //Tap gesture to dismiss the keyboard
    @IBAction func hideKeyboard(_ sender: AnyObject) {
        self.view.endEditing(false)
    }

       deinit {
        NotificationCenter.default.removeObserver(self);
    }
}

最佳答案

我已经创建了 UIViewController 的扩展和 scrollView 的创建方法。只需要调用 viewWillAppear() 和 viewDidDisappear()

extension UIViewController  {

    func registerForKeyboradDidShowWithBlock (scrollview:UIScrollView ,block: ((CGSize?)  -> Void)? = nil ){
        NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardDidShow, object: nil, queue: nil) { (notification) in
            if let userInfo = (notification as NSNotification).userInfo {
                if let keyboarRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
                    if self.view.findFirstResponder() != nil {
                        let  keyboarRectNew = self.view .convert(keyboarRect, to: self.view)
                        let scrollViewSpace = scrollview.frame.origin.y + scrollview.contentOffset.y
                        let textFieldRect:CGRect =  self.view.findFirstResponder()!.convert(self.view.findFirstResponder()!.bounds, to: self.view)
                        let textFieldSpace = textFieldRect.origin.y + textFieldRect.size.height
                        let remainingSpace =  self.view.frame.size.height - keyboarRectNew.size.height

                        if  scrollViewSpace + textFieldSpace > remainingSpace {
                            let gap =  scrollViewSpace + textFieldSpace - remainingSpace
                            scrollview .setContentOffset(CGPoint(x: scrollview.contentOffset.x, y: gap), animated: true)
                        }
                    }
                }
            }
            block?(CGSize.zero)

        }
    }

    func registerForKeyboardWillHideNotificationWithBlock ( scrollview:UIScrollView ,block: ((Void) -> Void)? = nil) {
        NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillHide, object: nil, queue: nil, using: { (notification) -> Void in
            scrollview.scrollRectToVisible(CGRect(x: 0, y: 0, width: 0, height: 0), animated: true)
            scrollview.contentOffset = CGPoint(x: 0, y: 0)
            scrollview.contentInset =  UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)
            scrollview.scrollIndicatorInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);

            block?()
        })
    }

    func deregisterKeyboardShowAndHideNotification (){

        NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
        NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)

        self.view.findFirstResponder()?.resignFirstResponder()
    }
}

我已经创建了 UIView 的扩展以在其中创建查找第一响应者方法。

extension UIView {
    func findFirstResponder() -> UIView? {
        if self.isFirstResponder {
            return self
        }
        for subview: UIView in self.subviews {
            let firstResponder = subview.findFirstResponder()
            if nil != firstResponder {
                return firstResponder
            }
        }
        return nil
    }   
}

关于ios - UIScrollView 在键盘关闭时停止滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41519044/

相关文章:

iOS 推送通知与杀死的应用程序 - swift

ios - iOS 7 中是否有蜂窝数据使用 API?

objective-c - UIScrollView 的 subview 中的水平 UISwipeGestureRecognizer? (UIScrollView 只需要识别垂直滚动)

ios - ImageView 的奇怪边框故障

ios - 使用 Swift 解析文本速度极慢

ios - 如何调试 "Thread 1: Fatal error: Index out of range in Swift"

ios - 为什么我的 Swift 程序停滞不前?

swift - 使用 UIButtons 在放大的 UIImageView 中左、右、上、下移动

ios - 许多应用程序,如 Tweetbot、Facebook 等,都允许您通过向下/向上拉动来关闭图像覆盖。这是如何实现的?

ios - Swift3 中的 NSIndexPath 初始化有何变化?