Scrollview 不适用于 xcode4(适用于 xcode3)
我是新手,如果这是一个愚蠢的疑问,请原谅我...... 我最近将我的项目从 xcode3 更改为 xcode4,现在出现了一些问题,我不知道为什么......
为了管理我的滚动视图的框架和内容大小,以及方向改变时它的变化,我我知道了:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
scrollView.frame = CGRectMake(0,0,480,300);
[scrollView setContentSize:CGSizeMake(480,560)];
}
else {
scrollView.frame = CGRectMake(0,0,320,460);
[scrollView setContentSize:CGSizeMake(320,560)];
}
[scrollView flashScrollIndicators];}
我认为使用 Xcode3,应用程序会在加载屏幕时调用此方法,因此它在每种情况下都能完美运行。
但在 Xcode4 中,应用程序在屏幕加载时不会调用此方法。它仅在方向改变时调用它(我不知道为什么存在这种差异)。所以,当屏幕加载时,我的滚动视图不起作用。仅当我更改设备的方向时它才开始工作。
然后,我尝试了这个:
- (void)viewDidLoad {
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft ||
[[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
scrollView.frame = CGRectMake(0,0,480,300);
[scrollView setContentSize:CGSizeMake(480,560)];
}
else {
scrollView.frame = CGRectMake(0,0,320,460);
[scrollView setContentSize:CGSizeMake(320,560)];
}
[scrollView flashScrollIndicators];
[super viewDidLoad];}
它显然正在工作,但是......发生了一些非常奇怪的事情......
如果我转到另一个视图(纵向方向),然后我将方向更改为横向,然后返回到第一个视图视图(在景观上),视图的自动调整大小蒙版变得疯狂。所有内容都位于右侧,就好像视图的宽度比实际宽度更大。而且我无法访问部分内容。但如果现在我将方向更改为纵向,一切都会再次正常,即使我再次转到横向。所以,只有当我从横向方向的另一个角度来看这个视图时,这是错误的。
这就是我从一个视图转到另一个视图的方式:
- (IBAction) btnUnVano:(id) sender {
PE2100UnVano *controller = [[PE2100UnVano alloc] initWithNibName:@"PE2100UnVano" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:controller animated:YES];
[controller release];}
我应该做什么?
Pd:抱歉我的英语
编辑
好,这部分现在已修复。我删除了 willAnimateRotationToInterfaceOrientation,现在它可以正常工作:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Si la orientación es horizontal..
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft ||
[[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
scrollView.frame = CGRectMake(0,0,480,300);
[scrollView setContentSize:CGSizeMake(480,560)];
}
// Si la orientación es vertical..
else {
scrollView.frame = CGRectMake(0,0,320,460);
[scrollView setContentSize:CGSizeMake(320,560)];
}
// Mostrar durante un instante los indicadores de scroll
[scrollView flashScrollIndicators];
// Retorna YES para soportar todas las orientaciones posibles
return YES;}
现在,还有另一个问题。在某些屏幕中,我使用以下代码返回到上一个屏幕:
- (IBAction) btnAtrasTabla:(id) sender {
[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];}
在这种情况下,自动调整大小的问题再次出现。我以纵向方式转到此屏幕,然后更改为横向,然后返回到上一个屏幕,而上一个屏幕显示的自动大小错误。然后,我改变方向,一切又正确了......有什么想法吗?
I'm a newbie, so forgive me if this is a silly doubt...
I've recently changed my project from xcode3 to xcode4, and now there are some problems, and I don't know why...
To manage the frame and the content size of my scrollview, and its changes if the orientation changed, I've got this:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
scrollView.frame = CGRectMake(0,0,480,300);
[scrollView setContentSize:CGSizeMake(480,560)];
}
else {
scrollView.frame = CGRectMake(0,0,320,460);
[scrollView setContentSize:CGSizeMake(320,560)];
}
[scrollView flashScrollIndicators];}
I think that with Xcode3, the app called this method when the screen was loaded, so it was working perfectly in every situation.
But in Xcode4, the app doesn't call this method when the screen loads. It only calls it when the orientation changes (I don't know why exists this difference). So, my scrollview doesn't work when the screen is loaded. It only starts to work when I change the orientation of the device.
Then, I tried this:
- (void)viewDidLoad {
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft ||
[[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
scrollView.frame = CGRectMake(0,0,480,300);
[scrollView setContentSize:CGSizeMake(480,560)];
}
else {
scrollView.frame = CGRectMake(0,0,320,460);
[scrollView setContentSize:CGSizeMake(320,560)];
}
[scrollView flashScrollIndicators];
[super viewDidLoad];}
It was apparently working, BUT... something very strange happens...
If I go to another view (on Portrait orientation), and then I change the orientation to Landscape, and then I return to the first view (on Landscape), the autosizing mask of the view goes crazy. All the content goes to the right side, as if the view had more width than it actually has. And I can't access part of the content. But if now I change the orientation to Portrait, everything is OK again, even if I go to Landscape again. So, it's only wrong when I come to the view from another view on Landscape orientation.
This is how I pass from a view to another view:
- (IBAction) btnUnVano:(id) sender {
PE2100UnVano *controller = [[PE2100UnVano alloc] initWithNibName:@"PE2100UnVano" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:controller animated:YES];
[controller release];}
What should I do?
Pd: Sorry for my English
EDIT
OK, this part is fixed now. I deleted the willAnimateRotationToInterfaceOrientation, and now it works correctly with this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Si la orientación es horizontal..
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft ||
[[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
scrollView.frame = CGRectMake(0,0,480,300);
[scrollView setContentSize:CGSizeMake(480,560)];
}
// Si la orientación es vertical..
else {
scrollView.frame = CGRectMake(0,0,320,460);
[scrollView setContentSize:CGSizeMake(320,560)];
}
// Mostrar durante un instante los indicadores de scroll
[scrollView flashScrollIndicators];
// Retorna YES para soportar todas las orientaciones posibles
return YES;}
Now, there's another problem. In some screens, I return to the previous screen with this code:
- (IBAction) btnAtrasTabla:(id) sender {
[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];}
In this case, the problem of the autosize appears again. I go to this screen on Portrait, I change to Landscape, I return to the previous, and the previous appears with a wrong autosize. Then, I change the orientation, and all is correct again... any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加到 rootViewController 的任何 viewController.view 都会从 rootViewController 本身继承其行为。
这意味着您只需要在 rootViewController 中启用 shouldAutorotateToInterfaceOrientation。
如果您将 viewController 显示为模态视图,则会有不同的行为。
因此,您需要在父级和模态视图本身中启用shouldAutorotateToInterfaceOrientation。
正确的架构应该可以解决您的问题,但我认为如果您无法清理代码,有一个解决方法:检查当前视图中的方向并向其他视图发送通知,以便设置它们的框架。请记住,此解决方法可能有用,但不建议这样做。
Any viewController.view added to your rootViewController inherits its behavior from the rootViewController itself.
This means you need to enable shouldAutorotateToInterfaceOrientation only in your rootViewController.
If you present your viewController as a modal view, you have different behaviors.
So you need to enable shouldAutorotateToInterfaceOrientation in your parent and in the modal view itself.
A correct architecture should solve your issue, but I think there is a workaround if you are not able to clean your code: check for the orientation in your current view and send a notification to other views, in order to set their frame. Remember that this workaround could be useful, but it is not recommended.
在贝佩的建议下,我最终做了以下事情。
在“子”视图上,当我离开它时,我发送一个通知:
然后,“父”视图是观察者:
这样做,当我们从“子”视图返回到“父”视图时,shouldAutorotateToInterfaceOrientation方法被称为。请记住,在此方法中,我调整了所有内容的大小以适合应用程序的当前方向。
Here is what I finally did, with the advice of Beppe.
On the 'child' view, when I leave it, I send a notification:
Then, the 'parent' view is the observer:
Doing this, when we return to the 'parent' view from the 'child' view, the shouldAutorotateToInterfaceOrientation method is called. Remember that in this method, I resize everything to fit the current orientation of the app.