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

use*_*389 5 uiscrollview uitextfield uitextview ios swift

我现在面临的问题是UIScrollView中有一个UITextField。当我单击 UITextField 时,我将显示 UIPickerView 和 UIToolBar。当 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()
}
Run Code Online (Sandbox Code Playgroud)

当我单击其他文本字段时,这些文本字段会在键盘出现时上升。但是如何处理 PickerTextField

对 UItextView 也有任何想法

谢谢。

Nui*_*ibb 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)
    }
Run Code Online (Sandbox Code Playgroud)