views:

39

answers:

1

I have a UIScrollview with an IBOutlet mapped to ganttScroller. This UIScrollView has a UIView in it. I made the UIView in IB and its width is 100.

I then start to add buttons to that UIView (mapped via an IBOutlet scrollContent)

float test = [scrollContent frame].size.width;
for (int i=0; i<15; i++) {
        UIButton *showButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  

        showButton.frame = CGRectMake(55.0 * i,  
                                        50.0,  
                                        50.0,  
                                        20.0);  
        [showButton setTitle:NSLocalizedString(@"test", @"") forStates:UIControlStateNormal];  
        [scrollContent addSubview:showButton];  
    }
    test = [scrollContent frame].size.width;
    [scrollContent sizeToFit];
    test =[scrollContent frame].size.width;

At the beginning I check the size of my scrollContent and it is indeed 100 (checked 'test' in the debugger), after adding the buttons it is again 100, and after sizeToFit it is still 100 (I would expect a lot larger since all those buttons were added... The buttons are shown correctly! I need the correct size for my gantScroller (UIScrollView)

What is wrong ?

+1  A: 

I don't think -[UIView sizeToFit] actually takes in to account its subviews. You'll need to simply calculate the correct width and assign that to the view's frame. You'll then also need to set the scroll view's contentSize property so that it scrolls correctly.

Probably the reason you see all the buttons despite the container being too small is that UIViews do not clip their subviews by default.

Daniel Dickison
Ok, thx I thought that contentSize would calculate that for me, what method do I need to calculate that correct width?
Glenn
When, in your particular case it's `CGRect frame = scrollContent.frame; frame.size.width = 14*55 + 50; scrollContent.frame = frame; scrollView.contentSize = frame.size;`
Daniel Dickison