【发布时间】:2019-04-11 21:18:01
【问题描述】:
我有一个带有 8 到 9 个文本编辑的视图控制器,用户必须填写它们才能保存到数据库,但它占用了我的很多屏幕,并且由于 iphone 屏幕的大小而没有显示一些 TE。然后我决定像这样添加一个 UIScrollView :
lazy var myScrollView : UIScrollView = {
let scrol = UIScrollView()
scrol.contentSize.height = 10000
scrol.backgroundColor = appBackgroundColor
scrol.translatesAutoresizingMaskIntoConstraints = false
return scrol
}()
...
view.addSubview(myScrollView)
myScrollView.addSubview(labelYear)
myScrollView.addSubview(labelAppTitle)
// then I added the constraints
NSLayoutConstraint.activate([
myScrollView.topAnchor.constraint(equalTo: view.topAnchor),
myScrollView.leftAnchor.constraint(equalTo: view.leftAnchor),
myScrollView.rightAnchor.constraint(equalTo: view.rightAnchor),
//enter code here
myScrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
labelAppTitle.leftAnchor.constraint(equalTo: myScrollView.leftAnchor,constant: 40),
labelAppTitle.topAnchor.constraint(equalTo: myScrollView.safeAreaLayoutGuide.topAnchor, constant: 10),
labelAppTitle.rightAnchor.constraint(equalTo:myScrollView.rightAnchor, constant: -40),
labelAppTitle.heightAnchor.constraint(equalToConstant: 90)
])
我有更多的文本编辑,但我不是为了节省空间而发布的。问题是它没有像我想要的那样向下滚动。我该怎么做?
谢谢
【问题讨论】:
-
查看之前的答案。 Programmatic UIScrollview with Autolayout。它将帮助解释如何将您的
UITextFields 添加到 scrollView 并确保它滚动。 -
此外,由于您有这么多文本字段,您可能需要考虑使用
UITableViewController,而不是为每个文本字段添加一个单元格,它可以免费为您提供键盘处理。
标签: swift scrollview