1
votes

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.

3
IMO you should use a collectionview instead? Also, what is the value of myScrollView.contentSize after this. Can you print it to see - Devster101
myScrollView.frame (0.0, 60.0, 375.0, 42.0) / myScrollView.contentSize (700.0, 42.0) - Dean Le
The content seems to ok then, the only other thing I can think of is maybe enabling the scroll. Add in myScrollView.isScrollEnabled = true - Devster101
well, actually it can be scrolled but just little. Even though I still have 3 or 4 more buttons, I cannot scroll to them. - Dean Le
Ah ok, Well let me get this straight, You want to display 6 buttons right. As your going < numberOfButton which will stop at 6. Also it must be a typo but you have an s at the end of numberOfButton when computing the contentSize. In any case, I think your problem is the fact your already using up 80points on the x axis on the first button. Causing the others to be off screen. Change the line where you set the frame to this: let frame = CGRect(x: 8 + (index * 96), y: 6, width: 80, height: 32). Ive added this as an answer so you can accept if it fixes you're issue - Devster101

3 Answers

2
votes

I think your problem is setting the X value on the first button. I have just tried this code and it works fine

    let numberOfButtons = 7
    for index in 0..<numberOfButtons {
        let button = UIButton()
        let frame = CGRect(x: 8 + (index * 96), y: 6, width: 80, height: 32)
        button.setTitle("Button \(index)", for: .normal)
        button.frame = frame
        button.titleLabel?.font = UIFont(name: "Museo Sans", size: 16)
        button.setTitleColor(UIColor.blue, for: .normal)

        myScrollView.addSubview(button)
    }
    myScrollView.contentSize = CGSize(width: 100*numberOfButton, height: 42)
1
votes

Change this

tagBar.contentSize = CGSize(width: 100*7, height: 42)

to

myScrollView.contentSize = CGSize(width: 100*7, height: 42)
0
votes

The answer that Rajeshkumar R gave you, or better use constraints. You can set a constraint between the left of myScrollView and the left your first button (a "leading space" constraint), then a leading space constraint between each button and the previous one, and when you finish looping, a trailing space from your last button to the myScrollView.

This way you don't have to calculate the contentSize yourself. It's also more extensible (for example, if you had buttons of different sizes, you would have to know each width, sum them all, then sum the margins between elements and between the first and the last one with the scrollView…).