iPhone - 实现 UIScrollView 的正确方法是什么?

发布于 2024-12-14 11:41:27 字数 980 浏览 3 评论 0原文

我有两个 UIViewController,每个都有自己的 nib 文件。每个控制器都有不同的用途。我们的想法是添加一个 UIScrollController 并向其中添加两个 UIViewController,以便用户可以轻松地在它们之间滚动。

Q1)这可能吗?或者我是否理解了 UIScollControllers 的全部目的不正确,如果是这样,最好的选择是什么。

我一直在互联网上闲逛,我能想到的最好的办法是:

ScoreViewController *scoreController = [[ScoreViewController alloc] initWithNibName:@"ScoreViewController" bundle:nil];
scoreController.view.frame = CGRectMake(0.0f, 0.0f, 960f, 640f);
[self.scrollview addSubview:scoreController.view];
[scoreController release];

SettingsViewController *settingsController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
settingsController.view.frame = CGRectMake(0.0f, 0.0f, 960f, 640f);
[self.scrollview addSubview:settingsController.view];
[settingsController release];

此代码输入到我的 RootViewController 类中。它似乎不起作用,因为我无法在我添加的两个 UIViewControllers(分数和设置)之间滚动。屏幕上显示的所有内容都是添加的第二个控制器 - SettingsViewController。

Q2) 为什么这个坏了?

谢谢。 :)

I have two UIViewControllers each with their own nib files. Each controller serves a different purpose. The idea was to add a UIScrollController and add both UIViewControllers to it so the user can easily scroll between them both.

Q1) Is this even possible? Or have I got the whole purpose of UIScollControllers incorrect, if so, what is the best alternative.

I have been poking around the internet and the best I can come up with is this:

ScoreViewController *scoreController = [[ScoreViewController alloc] initWithNibName:@"ScoreViewController" bundle:nil];
scoreController.view.frame = CGRectMake(0.0f, 0.0f, 960f, 640f);
[self.scrollview addSubview:scoreController.view];
[scoreController release];

SettingsViewController *settingsController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
settingsController.view.frame = CGRectMake(0.0f, 0.0f, 960f, 640f);
[self.scrollview addSubview:settingsController.view];
[settingsController release];

This code is entered inside my RootViewController class. It doesn't seem to work though as I can't scroll between the two UIViewControllers I have added (score and settings). All what is displayed on the screen is the second controller added - the SettingsViewController.

Q2) Why is this broken?

Thank you. :)

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

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

发布评论

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

评论(1

因为看清所以看轻 2024-12-21 11:41:27

实际上,您正确放置了第一个控制器,然后将第二个控制器放置在其上方。

settingsController.view.frame = CGRectMake(0.0f, 0.0f, 960f, 640f);
  1. 960x640 - 这不是输入坐标的正确方法(320x480是实际尺寸)双倍尺寸仅用于图像文件
  2. 0.0f,0.0f意味着x:0,y:0,所以在第二个控制器中您需要x:320 , y: 0

那么,你必须设置scrollView的contentSize

scrollview.contentSize = CGSizeMake(320*2, 480);

然后启用scrollView的分页;

scrollview.pagingEnabled = YES;

这应该有效:)

Actually you place the first controller correctly, then you put the second above it.

settingsController.view.frame = CGRectMake(0.0f, 0.0f, 960f, 640f);
  1. 960x640 - this is not the correct way to enter coordinates (320x480 is the actual size) double size is used only for image files
  2. 0.0f, 0.0f means x: 0, y: 0, so in the second controller you need x: 320, y: 0

then, you have to set the contentSize of the scrollView

scrollview.contentSize = CGSizeMake(320*2, 480);

Then enable the paging of the scrollView;

scrollview.pagingEnabled = YES;

And that should work :)

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