UIView 中的 UIButton

发布于 2024-09-25 01:37:03 字数 963 浏览 0 评论 0原文

我有一个 UIScrollview,其中 IBOutlet 映射到 ganttScroller。这个UIScrollView里面有一个UIView。我在IB中制作了UIView,其宽度为100。

然后我开始向该UIView添加按钮(通过IBOutlet滚动内容映射)。

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;

首先,我检查了滚动内容的大小,它确实是100(在调试器),添加按钮后它又是 100,在 sizeToFit 之后它仍然是 100(我期望更大,因为添加了所有这些按钮......按钮显示正确! 我需要我的 gantScroller (UIScrollView) 的正确尺寸

有什么问题吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给我一枪 2024-10-02 01:37:03

我不认为 -[UIView sizeToFit] 实际上考虑了它的子视图。您只需计算正确的宽度并将其分配给视图的框架。然后,您还需要设置滚动视图的 contentSize 属性,以便其正确滚动。

尽管容器太小,您仍能看到所有按钮的原因可能是 UIView 默认情况下不会剪辑其子视图。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文