【问题标题】:Swift move scroll view up when keyboard present键盘存在时快速向上移动滚动视图
【发布时间】:2020-05-03 00:24:37
【问题描述】:

我正在开发一个 iOS 应用程序,目前我的所有元素都在滚动视图中,当键盘出现时,我将视图向上移动 250 点。这解决了我的问题,但每个设备的键盘总是不同的尺寸。

如何检测我的文本字段离屏幕底部有多远以及键盘有多高?

【问题讨论】:

    标签: ios swift keyboard


    【解决方案1】:

    您应该注意显示和隐藏键盘的通知。之后,您可以获得确切的键盘大小,并移动或更改滚动视图的内容插图。这是一个示例代码:

    extension UIViewController {
        func registerForKeyboardDidShowNotification(scrollView: UIScrollView, usingBlock block: (NSNotification -> Void)? = nil) {
            NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardDidShowNotification, object: nil, queue: nil, usingBlock: { (notification) -> Void in
                let userInfo = notification.userInfo!
                let keyboardSize = userInfo[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue.size
                let contentInsets = UIEdgeInsetsMake(scrollView.contentInset.top, scrollView.contentInset.left, keyboardSize!.height, scrollView.contentInset.right)
    
                scrollView.scrollEnabled = true
                scrollView.setContentInsetAndScrollIndicatorInsets(contentInsets)
                block?(notification)
            })
        }
    
        func registerForKeyboardWillHideNotification(scrollView: UIScrollView, usingBlock block: (NSNotification -> Void)? = nil) {
            NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardWillHideNotification, object: nil, queue: nil, usingBlock: { (notification) -> Void in
                let contentInsets = UIEdgeInsetsMake(scrollView.contentInset.top, scrollView.contentInset.left, 0, scrollView.contentInset.right)
                scrollView.setContentInsetAndScrollIndicatorInsets(contentInsets)
                scrollView.scrollEnabled = false
                block?(notification)
            })
        }
    }
    
    extension UIScrollView {
        func setContentInsetAndScrollIndicatorInsets(edgeInsets: UIEdgeInsets) {
            self.contentInset = edgeInsets
            self.scrollIndicatorInsets = edgeInsets
        }
    }
    

    在您想要移动滚动视图的UIViewController 中,只需在viewDidLoad() 函数下添加下一行

    override func viewDidLoad() {
        super.viewDidLoad()
    
        registerForKeyboardDidShowNotification(scrollView)
        registerForKeyboardWillHideNotification(scrollView)
    }
    

    【讨论】:

    • 不错!这适用于 UITextField 但不适用于 UITextView 。知道为什么吗?
    【解决方案2】:

    我目前正在解决这个问题并找到了解决方案。首先,您需要向视图控制器添加一个通知,以识别键盘是否打开。为此,您需要在 viewDidLoad() 中注册此通知。

    override func viewDidLoad() { 
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow:", name: UIKeyboardWillChangeFrameNotification, object: nil)
    }
    

    当视图控制器从视图中移除时,不要忘记移除此通知。所以在 viewDidDisappear() 上删除这个通知。

     override func viewDidDisappear(animated: Bool) { 
         NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillChangeFrameNotification, object: nil)
     }
    

    最后一件事是在键盘打开或关闭时管理滚动视图。因此,首先您需要确定键盘高度。然后,为了获得非常流畅的动画效果,您可以使用键盘动画情绪和持续时间来为滚动视图设置动画。

    func keyboardDidShow(notification: NSNotification) {
        if let userInfo = notification.userInfo {
            let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
            let duration:NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
            let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
            let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue
            let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
            if endFrame?.origin.y >= UIScreen.mainScreen().bounds.size.height {
                //isKeyboardActive = false
                UIView.animateWithDuration(duration,
                delay: NSTimeInterval(0),
                options: animationCurve,
                animations: { 
                 // move scroll view height to 0.0 
                 },
                completion: { _ in
            })
            } else {
                //isKeyboardActive = true
                UIView.animateWithDuration(duration,
                delay: NSTimeInterval(0),
                options: animationCurve,
                animations: { 
                 // move scroll view height to    endFrame?.size.height ?? 0.0 
                 },
                completion: { _ in
            })
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      @noir_eagle 的答案似乎是正确的。

      但可能有更简单的解决方案。也许您可以尝试使用IQKeyboardManager。它允许您以简单无缝的方式处理这些键盘的事情。

      我认为你真的应该,至少,花几分钟看看它。

      【讨论】:

        猜你喜欢
        • 2016-05-07
        • 2015-12-22
        • 2015-10-28
        • 1970-01-01
        • 2018-05-21
        • 2012-10-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多