简体   繁体   中英

UIButtons in a UIView

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM