代码之家  ›  专栏  ›  技术社区  ›  Sean Zlatnik

想要能够手动滚动到键盘上方的文本字段

  •  0
  • Sean Zlatnik  · 技术社区  · 8 年前

    然而,在scrollview上启用滚动并不能解决问题;即使在这种情况下,它仍然不会对平移做出响应。使viewcontroller成为scrollview的代理和重写函数scrollViewDidScroll也是如此。如何让scrollview启用平移,尤其是仅在启用键盘时?

    由于发布了一个不太有效的解决方案,我想我会发布我的keyboardWillBeShown和KeyboardWillbehiden代码:

    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 回复  |  直到 8 年前
        1
  •  0
  •   Kiester    8 年前

    试试这个,

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

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

    希望这有帮助。

    固定调整

        2
  •  0
  •   Sean Zlatnik    8 年前

    我想出了如何得到我想要的。设置myScrollView。当键盘显示时,contentSize与屏幕加上键盘一样大,并将其缩小到隐藏键盘时的大小。然后将视图控制器设置为UIScrollViewDelegate,设置myScrollView。delegate=self,并实现scrollViewDidScroll,以便如果某个文本字段正在编辑,并且该文本字段不是空的,则更改哪个文本字段是第一个响应者。一旦你意识到设置的诀窍,那就太容易了。contentSize!