5

The problem I am facing now is that there is a UITextField in UIScrollView. When i clicks the UITextField, i am showing UIPickerView with UIToolBar. I need to move UITextField to up when PickerView presents.

Here is my design enter image description here

Here is my code

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

When i click other TextFields, those TextField goes up when keyboard presents. But what to do for PickerTextField

Any ideas for UItextView too

Thanks.

2 Answers 2

3

Swift 3 Version:

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)
}
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

I tried your code. Its not working for my condition. I am getting TextView y value as 93. Because i added TextView inside of UIView. U can see this in my design.
You should add your textView in scrollView and it will work then. You must inherit textViewDelegate in your view Controller as well as set the delegate of textView in storyboard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.