What I've got is a UIScrollView with a containerview inside it. This has a UITableViewcontroller embedded that contains static cells. I'd like to scroll the UIScrollView when I'm selecting a UITextField inside the static cells. I tried it two ways, 1. calculating the position, 2. calculating the cell index. Both ways I keep getting the same values when I'm selection an any UIField. Can someone help me to get this fixed?
I used this question: How do I scroll the UIScrollView when the keyboard appears?
func keyboardWasShown(notification : NSNotification){
var info = notification.userInfo
// var kbSize = info(UIKeyboa
if let userInfo = notification.userInfo {
if let kbSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: kbSize.height, right: 0)
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
//first approach
println("---- approach 1 ------")
var activeField = self.container?.groupField.superview?.superview
println(activeField!.frame.origin)
//prints (110.0, 5.0)
//second approach
println("---- approach 2 ------")
var cell = self.container?.groupField.superview?.superview as! UITableViewCell
var table = self.container?.view as! UITableView
var index = table.indexPathForCell(cell)
println("index: \(index?.row)")
//prints always 4
var aRect = self.view.frame
aRect.size.height -= kbSize.height
// println("aRect: \(aRect)")
if (!CGRectContainsPoint(aRect, activeField!.frame.origin) ) {
println("ActiveField: \(activeField!.frame.origin.y)")
var scrollPoint = CGPointMake(0.0, activeField!.frame.origin.y-kbSize.height)
scrollView.setContentOffset(scrollPoint, animated:true)
}
}
}
}