我在UIViewController中有一个UIScrollView,我期望它水平滚动。我通过循环以编程方式将按钮添加到ScrollView。循环之后,我将myScrollView.contentSize.width
设置为buttonWidth * numberOfButtons
。我也仔细检查,以确保contentSize大于scrollview的框架(在这种情况下,滚动视图的宽度为375)。UIScrollView无法水平滚动
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的值是多少。你可以打印它看到 – Devster101
myScrollView.frame(0.0,60.0,375.0,42.0)/ myScrollView.contentSize(700.0,42.0) –
内容似乎没问题,我唯一能想到的其他事情可能是启用滚动。添加在myScrollView.isScrollEnabled = true – Devster101