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.