繁体   English   中英

键盘出现时快速向上移动滚动视图

[英]Swift move scroll view up when keyboard present

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

我怎么能检测到我的文本字段离屏幕底部有多远以及键盘有多高?

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

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

我目前正在研究这个并找到了解决方案。 首先你需要向视图控制器添加一个通知来识别键盘是否打开。 为此,您需要在 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
        })
        }
    }
}

@noir_eagle 答案似乎是正确的。

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

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM