如何使 UIScrollView 尊重包含 UIView 的布局?

发布于 2024-10-03 11:48:27 字数 1591 浏览 0 评论 0原文

我使用 UIView 来控制视图的布局(以及视图控制器)。我希望 UIScrollView 仅使用垂直屏幕的一半。如果我使用屏幕的上半部分而不是下半部分,效果很好。

以下是 UIViewController 中的相关代码:

 - (void)loadView {
CGRect fullFrame = [[UIScreen mainScreen] applicationFrame]; 
//trying to put the scroll view on the bottom half of the screen, but does not work.
CGRect halfFrame = CGRectMake(0, fullFrame.size.height / 2 ,
    fullFrame.size.width, fullFrame.size.height / 2);
//use this instead for the scroll view to go to the top half of the screen (and work properly)
//CGRect halfFrame = CGRectMake(0, 0 , fullFrame.size.width, fullFrame.size.height / 2);

 UIScrollView* sv = [[UIScrollView alloc] initWithFrame:halfFrame]; 
[sv setContentSize:CGSizeMake(3 * halfFrame.size.width, halfFrame.size.height)];

CGRect stencilFrame = halfFrame;
UIView *leftView = [[UIView alloc] initWithFrame:stencilFrame];

stencilFrame.origin.x += stencilFrame.size.width;
UIView *centerView = [[UIView alloc] initWithFrame:stencilFrame];

stencilFrame.origin.x += stencilFrame.size.width;
UIView *rightView = [[UIView alloc] initWithFrame:stencilFrame];

//mix up the colors
[leftView setBackgroundColor:[UIColor redColor]];
[centerView setBackgroundColor:[UIColor greenColor]];
[rightView setBackgroundColor:[UIColor blueColor]];

//add them to the scroll view
[sv addSubview:leftView];
[sv addSubview:centerView];
[sv addSubview:rightView];

//turn on paging
[sv setPagingEnabled:YES];

UIView *containerView = [[UIView alloc]initWithFrame:fullFrame];
[containerView addSubview:sv];
[self setView:containerView];    
 }

提前感谢您的任何建议或帮助。

I'm using a UIView to control the layout of my view (along with a view controller). I want UIScrollView to only use half of the vertical screen. That works fine if I use the upper half of the screen, but not the bottom half.

Here's the relevant code from the UIViewController:

 - (void)loadView {
CGRect fullFrame = [[UIScreen mainScreen] applicationFrame]; 
//trying to put the scroll view on the bottom half of the screen, but does not work.
CGRect halfFrame = CGRectMake(0, fullFrame.size.height / 2 ,
    fullFrame.size.width, fullFrame.size.height / 2);
//use this instead for the scroll view to go to the top half of the screen (and work properly)
//CGRect halfFrame = CGRectMake(0, 0 , fullFrame.size.width, fullFrame.size.height / 2);

 UIScrollView* sv = [[UIScrollView alloc] initWithFrame:halfFrame]; 
[sv setContentSize:CGSizeMake(3 * halfFrame.size.width, halfFrame.size.height)];

CGRect stencilFrame = halfFrame;
UIView *leftView = [[UIView alloc] initWithFrame:stencilFrame];

stencilFrame.origin.x += stencilFrame.size.width;
UIView *centerView = [[UIView alloc] initWithFrame:stencilFrame];

stencilFrame.origin.x += stencilFrame.size.width;
UIView *rightView = [[UIView alloc] initWithFrame:stencilFrame];

//mix up the colors
[leftView setBackgroundColor:[UIColor redColor]];
[centerView setBackgroundColor:[UIColor greenColor]];
[rightView setBackgroundColor:[UIColor blueColor]];

//add them to the scroll view
[sv addSubview:leftView];
[sv addSubview:centerView];
[sv addSubview:rightView];

//turn on paging
[sv setPagingEnabled:YES];

UIView *containerView = [[UIView alloc]initWithFrame:fullFrame];
[containerView addSubview:sv];
[self setView:containerView];    
 }

Thank you in advance for any advice or help.

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

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

发布评论

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

评论(3

梦魇绽荼蘼 2024-10-10 11:48:28

我想通了。问题的关键在于滚动视图中的视图是使用与滚动视图本身相同的框架来初始化的。当使用halfFrame初始化scrollView时,原点是(0,全屏尺寸的一半),这是可以的,因为它是相对于应用程序窗口本身的。然而,放置在scrollView内部的视图(如leftView)被初始化为halfFrame,但在这种情况下,原点是相对于scrollView的,有效地将它们放置在滚动视图之外。屏幕。将原点设置为 (0,0) 可以解决此问题:

CGRect stencilFrame = CGRectMake(0, 0, fullFrame.size.width , fullFrame.size.height / 2);

I figured it out. The crux of the problem is that views within the scroll view are initialized with the same frame as the scroll view itself. When the scrollView is initialized with halfFrame, the origin is (0, half the full screen size), which is ok since that is relative to the application window itself. However, the views that are put inside the scrollView (like leftView) are initialized to halfFrame, but in this case the origin is relative to the scrollView, effectively placing them off the screen. Setting the origin to (0,0) fixes this:

CGRect stencilFrame = CGRectMake(0, 0, fullFrame.size.width , fullFrame.size.height / 2);

错々过的事 2024-10-10 11:48:28

contentSize 必须包含滚动视图内视图的矩形。即,其中所有可滚动控件的总大小。 UIScrollView 的框架决定了需要多少滚动才能让用户浏览所有内容。

contentSize must contain the rectangle of the view inside the scroll view. That is, the total size of all scrollable controls within. The frame of the UIScrollView decides how much scrolling is needed to let the user browse everything.

镜花水月 2024-10-10 11:48:28

如果您有导航栏或选项卡栏,则无法使用“全框架”。一般来说,使用 [UIScreen mainScreen] 获取布局信息的代码可能是错误的。

此外,如果(例如)正在进行呼叫或启用网络共享,状态栏可以更改大小。

相反,对全帧使用任何合理的值并启用自动调整大小:

CGRect fullFrame = {{0,0}, {320,480}};

...

sv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;

编辑:您可能还需要子类化 UIScrollView 并实现 -setFrame:,以便它还设置内容大小和-layoutSubviews 进行正确的布局。

You don't have the "full frame" available if you have a nav bar or a tab bar. In general, code that uses [UIScreen mainScreen] for layout information is probably wrong.

Additionally, the status bar can change size if (for example) a call is in progress or tethering is enabled.

Instead, use any sane value for full frame and enable autoresizing:

CGRect fullFrame = {{0,0}, {320,480}};

...

sv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;

EDIT: You also probably need to subclass UIScrollView and implement -setFrame: so that it also sets the content size and -layoutSubviews to do the correct layout.

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