0

我是新手,如果这是一个愚蠢的疑问,请原谅我...我最近将我的项目从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];}

在这种情况下,自动调整大小的问题再次出现。我在纵向上转到此屏幕,然后更改为横向,然后返回上一个,并且上一个出现错误的自动调整大小。然后,我改变方向,一切又是正确的......有什么想法吗?

4

2 回答 2

0

添加到 rootViewController 的任何 viewController.view 都会从 rootViewController 本身继承其行为。

[self.view addSubview:anotherViewController.view];

这意味着您只需要在您的 rootViewController 中启用 shouldAutorotateToInterfaceOrientation。

如果将 viewController 呈现为模态视图,则会有不同的行为。

[self presentModalViewController:anotherViewController animated:YES];

所以你需要在你的父级和模态视图本身中启用 shouldAutorotateToInterfaceOrientation 。

正确的架构应该可以解决您的问题,但我认为如果您无法清理代码,则有一种解决方法:检查当前视图中的方向并向其他视图发送通知,以设置它们的框架。请记住,此解决方法可能很有用,但不建议这样做。

于 2012-01-31T16:43:45.397 回答
0

在 Beppe 的建议下,这就是我最终所做的。

在“孩子”视图上,当我离开它时,我会发送一个通知:

[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"resize" object:nil userInfo:nil];

然后,“父”视图是观察者:

- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shouldAutorotateToInterfaceOrientation:) name:@"resize" object:nil];
[super viewDidLoad];}

这样做,当我们从“子”视图返回“父”视图时,会调用 shouldAutorotateToInterfaceOrientation 方法。请记住,在这种方法中,我会调整所有内容的大小以适应应用程序的当前方向。

于 2012-02-01T21:14:16.933 回答