1
votes

Got a strange problem which I dont understand.

I set the content size to a CGSize I created called newSize. I am adding buttons in a row in the UIScrollView that goes outside of the frame of the scrollview and changing the content size to match the new width + 400 for insurance.

    newSize.width = contentSizeWidth+400;

    NSLog(@"newSize width: %f", newSize.width);

    [_scrollViewOutlet addSubview:button];

    [_scrollViewOutlet setContentSize:newSize];

    NSLog(@"scrollviewOutlet Content Width: %f", _scrollViewOutlet.contentSize.width);

the NSLog prints out 768. scrolling is enabled in the scrollview in interface builder but I cannot scroll the scrollview? What could the problem be?

3
How do you calculate contentSizeWidth?Marcus Adams
_scrollViewOutlet.contentSize.width = 768.00 and Scrollview frame width is 400jimbob
So, contentSizeWidth has a hard-coded value?Marcus Adams
it runs through an array of buttons and the contentsizewidth increases like: contentSizeWidth = (20+button.frame.size.width) * i; to create a gap on 20 pixels in between each button within the scrollview.jimbob
Where are you calling the above code? viewDidLoad?Marcus Adams

3 Answers

1
votes

This may help: add buttons before you provide contentSize.

// add all buttons
// create contentSizeWidth presumably by adding all buttons height
contentSizeWidth = contentSizeWidth + buttonHeight;// repeat this for every button  
 [_scrollViewOutlet addSubview:button];

// create newSize
newSize.width = contentSizeWidth+400;

// set content size
  [_scrollViewOutlet setContentSize:newSize];
1
votes

I resolved the issue. It was an autolayout tick box that needed unticking in the view.

0
votes

Mayble the proble is that the width of the frame is equal to the width of the contentSize.