在一个简单的测试应用程序(没有添加代码,只是故事板)中,UIScrollView
会自动滚动显示下一个文本字段,当它成为第一个响应者时(它不考虑键盘高度,但我'我知道修复它的方法。但是,在我更复杂的应用程序中,这种内置滚动永远不会发生。否则scrollView功能完美。
我在Swift中编码,但如果你在Obj-C中有建议,我可以翻译。谢谢!
编辑:我可以确认这是因为以编程方式删除和重新添加UIscrollView的子视图。既然我需要为功能而做,那么如何恢复自动滚动的任何建议?
编辑:这是测试应用版本的代码,不会自动滚动。我用故事板布置了视图和字段,然后以编程方式删除并重新添加。
class ScrollViewController: UIViewController {
@IBOutlet weak var field1: UITextField!
@IBOutlet weak var field2: UITextField!
@IBOutlet weak var field3: UITextField!
@IBOutlet weak var field4: UITextField!
@IBOutlet weak var view1: UIView!
@IBOutlet weak var view2: UIView!
@IBOutlet weak var view3: UIView!
@IBOutlet weak var view4: UIView!
@IBOutlet weak var scrollView: UIScrollView!
var fields = [UITextField]()
var views = [UIView]()
override func viewDidLoad() {
super.viewDidLoad()
fields = [field1, field2, field3, field4]
views = [view1, view2, view3, view4]
for v in views {
v.removeFromSuperview()
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
var vConstraints = "V:|"
for v in views {
scrollView.addSubview(v)
scrollView.addConstraint(NSLayoutConstraint(item: v, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
let vIndex = find(views, v)! + 1
vConstraints += "-8-[v\(vIndex)]"
}
vConstraints += "-8-|"
scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(vConstraints, options: NSLayoutFormatOptions(0), metrics: nil, views: ["v1": view1, "v2": view2, "v3": view3, "v4": view4]))
self.view.layoutIfNeeded()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}