0
votes

I have a uiscrollview which does NOT have same height as the main view. I have a uitextfield and a button outside this scrollview. I enter a value in there and on clicking the button, it creates another textfield inside scrollview and then if i press again, it will create another textfield below the previous created textfield. Now, I am trying to scroll the view to the newly created textfield which should be visible above the keyboard view. I have tried the following. It is not working:

func keyboardWillShow() {
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
let textField: UITextField = self.scrollView!.viewWithTag(rowsCount) as! UITextField
let newRect: CGRect = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, textField.frame.width, textField.frame.height)
let newFrame = self.view.convertRect(newRect, fromView: nil)
keyboardFrame = self.view.convertRect(keyboardFrame, fromView: nil)
//          contentInset.bottom = newRect.origin.y + newRect.height
            var viewRect: CGRect = self.view.frame
            viewRect.size.height = viewRect.height - keyboardFrame.height - 40.0

            if (!CGRectContainsPoint(viewRect, (newFrame.origin))) {
                self.scrollView.scrollRectToVisible((newFrame), animated: true)
            }
}

P.S.: rowsCount increases everytime a new textfield is created.

1
Not getting exact requirement!! When you press button then it will create new text field, So do you want that textfield as active textfield(keyboard is presented for it) ?Ketan Parmar
I don't want active textfield because active textfield will always be the one where I am entering the value(The one that is outside the uiscrollview). What I want is the scroll view to scroll to the newly created textfield.Karanveer Singh

1 Answers

0
votes

You can do like,

 scrollView.scrollRectToVisible(CGRect(x: x, y: y, width: 1, height: 1), animated: true)

 // or

 scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true)

Pass your x and y to this on button click.