在选择的 UITextField 上滚动 UIScrollView,无法获得正确的尺寸
Scroll UIScrollView on selection UITextField, can't get the right sizes
我得到的是一个 UIScrollView,里面有一个 containerview。它嵌入了一个包含静态单元格的 UITableViewcontroller。当我在静态单元格中选择 UITextField 时,我想滚动 UIScrollView。我尝试了两种方法,1.计算位置,2.计算单元格索引。当我选择任何 UIField 时,这两种方式都会得到相同的值。有人可以帮我解决这个问题吗?
我用了这个问题:
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)
}
}
}
}
这是一个愚蠢的错误,我选择了一个默认字段作为活动字段
我替换了:
var activeField = self.container?.groupField.superview?.superview
致:
var activeField = self.container?.activeField.superview?.superview
我得到的是一个 UIScrollView,里面有一个 containerview。它嵌入了一个包含静态单元格的 UITableViewcontroller。当我在静态单元格中选择 UITextField 时,我想滚动 UIScrollView。我尝试了两种方法,1.计算位置,2.计算单元格索引。当我选择任何 UIField 时,这两种方式都会得到相同的值。有人可以帮我解决这个问题吗?
我用了这个问题: 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)
}
}
}
}
这是一个愚蠢的错误,我选择了一个默认字段作为活动字段
我替换了:
var activeField = self.container?.groupField.superview?.superview
致:
var activeField = self.container?.activeField.superview?.superview