简体   繁体   English

UIView中的UIButton

[英]UIButtons in a UIView

I have a UIScrollview with an IBOutlet mapped to ganttScroller. 我有一个UIScrollview,其中IBOutlet映射到ganttScroller。 This UIScrollView has a UIView in it. 此UIScrollView中有一个UIView。 I made the UIView in IB and its width is 100. 我在IB中制作了UIView,其宽度为100。

I then start to add buttons to that UIView (mapped via an IBOutlet scrollContent) 然后,我开始向该UIView添加按钮(通过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) 一开始,我检查一下我的scrollContent的大小,它的确是100(在调试器中选中了“测试”),在添加按钮之后,它又是100,在sizeToFit之后,它仍然是100(由于所有这些按钮已添加...按钮显示正确!我需要gantScroller(UIScrollView)的正确大小

What is wrong ? 怎么了 ?

I don't think -[UIView sizeToFit] actually takes in to account its subviews. 我不认为-[UIView sizeToFit]实际上会考虑其子视图。 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. 然后,您还需要设置滚动视图的contentSize属性,以便其正确滚动。

Probably the reason you see all the buttons despite the container being too small is that UIViews do not clip their subviews by default. 尽管容器太小,但您可能看到所有按钮的原因可能是UIView默认情况下不会裁剪其子视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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