【发布时间】:2017-03-08 14:10:52
【问题描述】:
我在 UIViewController 中有一个 UIScrollView,我希望它可以水平滚动。我通过循环以编程方式将按钮添加到 ScrollView。在循环之后,我将myScrollView.contentSize.width 设置为buttonWidth * numberOfButtons。我还仔细检查以确保 contentSize 大于滚动视图的框架(在这种情况下,滚动视图的宽度为 375)。
let numberOfButton = 7
for index in 0..<numberOfButton {
let button = UIButton()
let frame = CGRect(x: 80 + (index * 80), y: 6, width: 60, height: 32)
button.setTitle("Button" + String(index), forState: .Normal)
button.frame = frame
button.titleLabel?.font = UIFont(name: "Museo Sans", size: 16)
button.setTitleColor(UIColor.blueColor(), forState: .Normal)
myScrollView.addSubview(button)
}
myScrollView.contentSize = CGSize(width: 100*numberOfButtons, height: 42)
当我运行代码时,它只出现在 Button3(有 7 个按钮)上,我无法滚动到最后。但是,当我设置myScrollView.bounces = true 时,我可以拖动滚动视图并查看其他按钮,但它会弹回原始状态。任何帮助将不胜感激。
【问题讨论】:
-
IMO 你应该使用collectionview 代替吗?另外,在此之后 myScrollView.contentSize 的值是多少。能打印出来看看吗
-
myScrollView.frame (0.0, 60.0, 375.0, 42.0) / myScrollView.contentSize (700.0, 42.0)
-
内容似乎还可以,我唯一能想到的可能是启用滚动。添加 myScrollView.isScrollEnabled = true
-
嗯,实际上它可以滚动,但只是很小。即使我还有 3 或 4 个按钮,我也无法滚动到它们。
-
啊,好吧,让我直说吧,你想正确显示 6 个按钮。当您前进时
标签: ios swift uiscrollview