I have a UIScrollView inside a UIViewController and I expect it to be scrolled horizontally. I programmatically add button to the ScrollView by a loop. After the loop, I set the myScrollView.contentSize.width
to be buttonWidth * numberOfButtons
. I also double check to make sure that contentSize is bigger than the scrollview's frame (in this case the scrollview has width of 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)
When I run the code, it only appears to the Button3 (there are 7 buttons) and I cannot scroll it to the end. However, when I set myScrollView.bounces = true
I can drag the scrollview around and see other buttons but it will bounce back to the original state. Any help would be much appreciated.