想要手动滚动以拉动键盘上方的文本字段

时间:2017-08-01 07:14:25

标签: ios swift3 uiscrollview

问题显然是当用户发现键盘正在阻止他们想要输入的文本字段时,而不是点击返回,或关闭键盘并点击下一个字段时,他们会尝试平移到下一个字段。由于我的内容与iPad的大小相匹配,因此当用户尝试平移时,滚动视图不会自动滚动。老实说,除非键盘在屏幕上,否则我不希望它滚动。

但是,在滚动视图上启用滚动并不能解决问题;即使在这种情况下,它仍然不会响应平移。也没有使viewcontroller成为scrollview的委托并覆盖函数scrollViewDidScroll。如何启用滚动视图以启用平移,尤其是仅在启用键盘时?

由于发布的解决方案不太有用,我想我会发布我的keyboardWillBeShown和keyboardWillBeHidden代码:

func keyboardWillBeShown(_ sender: Notification)
{
    self.myScrollView.isScrollEnabled = true

    let info: NSDictionary = (sender as NSNotification).userInfo! as NSDictionary
    let value: NSValue = info.value(forKey: UIKeyboardFrameBeginUserInfoKey) as! NSValue
    let keyboardSize: CGSize = value.cgRectValue.size
    let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
    self.myScrollView.contentInset = contentInsets
    self.myScrollView.scrollIndicatorInsets = contentInsets
    self.myScrollView.scrollRectToVisible(self.myScrollView.frame, animated: true)
    var aRect: CGRect = self.view.frame
    aRect.size.height -= keyboardSize.height
    let activeTextFieldRect: CGRect? = activeField?.frame
    let activeTextFieldOrigin: CGPoint? = activeTextFieldRect?.origin
    if activeTextFieldOrigin != nil
    {
        if (!aRect.contains(activeTextFieldOrigin!))
        {
            let scrollpoint : CGPoint = CGPoint(x: 0.0, y: activeField!.frame.origin.y - keyboardSize.height)
            self.myScrollView.setContentOffset(scrollpoint, animated: true)//.scrollRectToVisible((activeField?.frame)!, animated: true)
        }
    }

}

func keyboardWillBeHidden(_ sender: Notification)
{
    myScrollView.isScrollEnabled = false
    let contentInsets: UIEdgeInsets = UIEdgeInsets.zero
    myScrollView.contentInset = contentInsets
    myScrollView.scrollIndicatorInsets = contentInsets
}

2 个答案:

答案 0 :(得分:0)

试试这个,

声明要使用的变量

var originalViewCGRect: CGRect?
var originalOffset: CGPoint!

viewDidLoad添加键盘观察器

    NotificationCenter.default.addObserver(self
        , selector: #selector(keyboardWillAppear(_:))
        , name: NSNotification.Name.UIKeyboardWillShow
        , object: nil)
    NotificationCenter.default.addObserver(self
        , selector: #selector(keyboardWillDisappear(_:))
        , name: NSNotification.Name.UIKeyboardWillHide
        , object: nil)

最后,添加这些功能

    func keyboardWillAppear(_ notification: Foundation.Notification){
        self.scrollView.scrollRectToVisible(self.scrollView.frame, animated: true)
        let keyboardSize:CGSize = ((notification as NSNotification).userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue.size
        let insets = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0)
        self.scrollView.contentInset = insets
    }


    func keyboardWillDisappear(_ notification: Foundation.Notification){
        let beginFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
        let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let delta = (endFrame.origin.y - beginFrame.origin.y)
        self.view.frame = self.originalViewCGRect!
        self.scrollView.setContentOffset(CGPoint(x: 0, y: max(self.scrollView.contentOffset.y - delta, 0)), animated: true)
    }

总之,当显示和/或隐藏键盘时,这将调整滚动视图的内容插入。

希望这有帮助。

修改 修正了调整

答案 1 :(得分:0)

我想出了如何得到我想要的东西。将键盘显示时,将myScrollView.contentSize设置为与屏幕和键盘一样大,并将大小缩小到隐藏键盘时的大小。然后使视图控制器成为UIScrollViewDelegate,设置myScrollView.delegate = self,并实现scrollViewDidScroll,这样如果文本字段正在编辑,并且该文本字段不是空白,则更改哪个文本字段是第一个响应者。一旦你意识到设置.contentSize的技巧,你就可以了!