ios - 以编程方式将按钮添加到 UIScrollView Swift 5
问题描述
我正在尝试以编程方式在网格中添加一些按钮,其中行数超过了屏幕大小,所以我希望它们在 UIScrollView 中。
我的最小工作示例如下:
class ViewController: UIViewController {
//center label and down a bit
let label = UILabel(frame: CGRect(x: UIScreen.main.bounds.width/2 - 100, y: 50, width: 200, height: 200))
// start scroll view underneath label (why not y: 250?)
let scrollView = UIScrollView(frame: CGRect(x: 0, y: 50, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
override func viewDidLoad() {
super.viewDidLoad()
// add label to view
self.view.addSubview(label)
// add Scrollview
self.view.addSubview(scrollView)
// uncommenting the following also doesn't work
//scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: 4000)
// add buttons to scroll view in 5x21 grid
var idx = 1
for row in 0...20 {
for col in 0...4 {
let button = UIButton(frame: CGRect(x: col*50, y: row*50 + 200, width: 50, height: 50))
button.backgroundColor = .gray
button.setTitle("\(idx)", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
// add button to scroll view
scrollView.addSubview(button)
idx += 1
}
}
}
@objc func buttonAction(sender: UIButton!) {
label.text = sender.title(for: .normal)
}
}
不幸的是,滚动不起作用。即使我明确设置了更大的垂直内容大小(参见注释行)//scrollView.contentSize
我想知道它是否与为固定按钮设置 x 和 y 值有关?但是,我还能如何将它们排列在网格中呢?
那么,如何在获得正常工作的 ScrollView 时获得我想要的按钮对齐?
解决方案
你真的应该使用自动布局!!!
到目前为止,将按钮(或任何视图)添加到滚动视图的最简单方法 - 在您要求的网格布局中 - 并让它自动确定滚动区域是堆栈视图。
这是一个简单的例子:
class ViewController: UIViewController {
let label = UILabel()
let scrollView = UIScrollView()
override func viewDidLoad() {
super.viewDidLoad()
// start with some text in the label
label.text = "Tap a button"
// center the text in the label
label.textAlignment = .center
// so we can see the frames
label.backgroundColor = .yellow
scrollView.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
// create a vertical stack view to hold the rows of buttons
let verticalStackView = UIStackView()
verticalStackView.axis = .vertical
// we're going to use auto-layout
label.translatesAutoresizingMaskIntoConstraints = false
scrollView.translatesAutoresizingMaskIntoConstraints = false
verticalStackView.translatesAutoresizingMaskIntoConstraints = false
// add label to view
self.view.addSubview(label)
// add Scrollview to view
self.view.addSubview(scrollView)
// add stack view to scrollView
scrollView.addSubview(verticalStackView)
// now let's create the buttons and add them
var idx = 1
for row in 0...20 {
// create a "row" stack view
let rowStack = UIStackView()
// add it to the vertical stack view
verticalStackView.addArrangedSubview(rowStack)
for col in 0...4 {
let button = UIButton()
button.backgroundColor = .gray
button.setTitle("\(idx)", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
// add button to row stack view
rowStack.addArrangedSubview(button)
// buttons should be 50x50
NSLayoutConstraint.activate([
button.widthAnchor.constraint(equalToConstant: 50.0),
button.heightAnchor.constraint(equalToConstant: 50.0),
])
idx += 1
}
}
// finally, let's set our constraints
// respect safe-area
let safeG = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
// constrain label
// 50-pts from top
// 80% of the width
// centered horizontally
label.topAnchor.constraint(equalTo: safeG.topAnchor, constant: 50.0),
label.widthAnchor.constraint(equalTo: safeG.widthAnchor, multiplier: 0.8),
label.centerXAnchor.constraint(equalTo: safeG.centerXAnchor),
// constrain scrollView
// 50-pts from bottom of label
// Leading and Trailing to safe-area with 8-pts "padding"
// Bottom to safe-area with 8-pts "padding"
scrollView.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 50.0),
scrollView.leadingAnchor.constraint(equalTo: safeG.leadingAnchor, constant: 8.0),
scrollView.trailingAnchor.constraint(equalTo: safeG.trailingAnchor, constant: -8.0),
scrollView.bottomAnchor.constraint(equalTo: safeG.bottomAnchor, constant: -8.0),
// constrain vertical stack view to scrollView Content Layout Guide
// 8-pts all around (so we have a little "padding")
verticalStackView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor, constant: 8.0),
verticalStackView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor, constant: 8.0),
verticalStackView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor, constant: -8.0),
verticalStackView.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor, constant: -8.0),
])
}
@objc func buttonAction(sender: UIButton!) {
label.text = sender.title(for: .normal)
}
}
这是结果:
然后我们将向下滚动并点击不同的按钮:
如果您想要按钮之间的间距,您可以在末尾添加类似这样的内容viewDidLoad()
:
// suppose we want 8-pts spacing between the buttons?
verticalStackView.spacing = 8.0
verticalStackView.arrangedSubviews.forEach { v in
if let stack = v as? UIStackView {
stack.spacing = 8.0
}
}
现在看起来像这样:
如果您希望按钮网格水平居中,或者您希望间距(或按钮大小)调整以适应,有几种不同的方法可以做到这一点......您没有在问题中描述您的最终目标,但这应该是一个很好的起点。
推荐阅读
- amazon-web-services - 如何提供对 AWS Web 控制台的有限访问?
- python-3.x - 如何删除或跳过文本文件中的行列表并在新文本文件中打印剩余的行?
- r - 从R中的向量列表中删除特定元素
- css - 如何将自定义样式设置为语义 ui 组件,reactjs
- javascript - 在 ReactJs 中将值从一个选项卡传递到另一个选项卡
- matlab - 如何使用 BreakPlot 函数避免箱线图出现不必要的空白?
- java - 通知操作意图未触发
- cassandra - 关于 Cassandra 关于键、分区的文档(草率,仍然令人困惑)
- html - 如何让 HTML 视频始终播放?
- javascript - 使用 react-router-dom v5 在路由更改前后做一些事情