当键盘出现在swift中时滚动UITextView

Posted

技术标签:

【中文标题】当键盘出现在swift中时滚动UITextView【英文标题】:Scroll UITextView when keyboard appears in swift 【发布时间】:2015-03-04 05:55:33 【问题描述】:

我现在面临的问题是UIScrollView中有一个UITextField。 当我单击 UITextField 时,我正在使用 UIToolBar 显示 UIPickerView。当 PickerView 出现时,我需要将 UITextField 向上移动。

这是我的设计

这是我的代码

 func keyboardWillShow(notification: NSNotification)

    let userInfo = notification.userInfo
    if let info = userInfo
    
        let animationDurationObject =
        info[UIKeyboardAnimationDurationUserInfoKey] as NSValue

        let keyboardEndRectObject =
        info[UIKeyboardFrameEndUserInfoKey] as NSValue

        var animationDuration = 0.0
        var keyboardEndRect = CGRectZero

        animationDurationObject.getValue(&animationDuration)
        keyboardEndRectObject.getValue(&keyboardEndRect)

        let window = UIApplication.sharedApplication().keyWindow

        keyboardEndRect = view.convertRect(keyboardEndRect, fromView: window)

        let intersectionOfKeyboardRectAndWindowRect =
        CGRectIntersection(view.frame, keyboardEndRect);

        UIView.animateWithDuration(animationDuration, animations: [weak self] in

            self!.scrollView.contentInset = UIEdgeInsets(top: 0,
                left: 0,
                bottom: intersectionOfKeyboardRectAndWindowRect.size.height,
                right: 0)
            self?.scrollView.scrollRectToVisible(self!.activeTextField.frame, animated: true)
        )
    


func keyboardWillHide(notification: NSNotification)

    let userInfo = notification.userInfo

    if let info = userInfo
        let animationDurationObject =
        info[UIKeyboardAnimationDurationUserInfoKey]
            as NSValue

        var animationDuration = 0.0;

        animationDurationObject.getValue(&animationDuration)

        UIView.animateWithDuration(animationDuration, animations: 
            [weak self] in
            self?.scrollView.contentInset = UIEdgeInsetsZero
            self?.scrollView.scrollIndicatorInsets = UIEdgeInsetsZero
        )
    


func textFieldShouldReturn(textField: UITextField) -> Bool

    textField.resignFirstResponder()
    return true


func textFieldShouldBeginEditing(textField: UITextField) -> Bool

    var returnVal = true
    self.activeTextField = textField
    if textField == self.pickerTextField
    
        returnVal = false
        var yval:CGFloat = self.view.bounds.size.height - 206
        self.pickerContainerView.frame = CGRectMake(0, yval, self.view.bounds.size.width, self.pickerContainerView.frame.size.height)
        self.pickerContainerView.hidden = false
        self.view.addSubview(self.pickerContainerView)
    
    return returnVal


func textFieldDidEndEditing(textField: UITextField) 
    self.activeTextField = nil
    textField.resignFirstResponder()

当我单击其他文本字段时,这些文本字段会在键盘出现时上升。但是 PickerTextField 怎么办

还有关于 UItextView 的任何想法

谢谢。

【问题讨论】:

【参考方案1】:

Swift 3 版本:

func textViewDidBeginEditing(_ textView: UITextView) 
    let scrollPoint : CGPoint = CGPoint.init(x:0, y:textView.frame.origin.y)
    self.scrollView.setContentOffset(scrollPoint, animated: true)


func textViewDidEndEditing(_ textView: UITextView) 
    self.scrollView.setContentOffset(CGPoint.zero, animated: true)

【讨论】:

【参考方案2】:
 You can try like below with textView delegate - 

 // MARK: UITextViewDelegate
    func textViewDidBeginEditing(textView: UITextView) 
        var scrollPoint : CGPoint = CGPointMake(0, self.textView.frame.origin.y)
        self.scrollView.setContentOffset(scrollPoint, animated: true)
    

    func textViewDidEndEditing(textView: UITextView) 
        self.scrollView.setContentOffset(CGPointZero, animated: true)
    

【讨论】:

我试过你的代码。它不适合我的情况。我将 TextView y 值设为 93。因为我在 UIView 中添加了 TextView。你可以在我的设计中看到这一点。 你应该在scrollView中添加你的textView,然后它就会工作。您必须在视图控制器中继承 textViewDelegate 并在情节提要中设置 textView 的委托。

以上是关于当键盘出现在swift中时滚动UITextView的主要内容,如果未能解决你的问题,请参考以下文章

当键盘出现在 iOS 7 中时如何调整 UITextview 的大小

当键盘出现在 iOS 中时启用滚动内容

当键盘出现在 Swift 中时 UITextField 向上移动

当虚拟键盘出现在本机反应中时如何自动向上滚动屏幕(在android上)

当键盘出现时视图仅大于屏幕时,如何在 iOS 中滚动?

UITextView 避免键盘滚动