【问题标题】:Scrollview not working on xcode4 (worked on xcode3)滚动视图不适用于 xcode4(适用于 xcode3)
【发布时间】:2012-01-31 14:35:48
【问题描述】:

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

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

【问题讨论】:

  • 在这种情况下,当您使用 presentModalViewController 时,您的 ViewController 会发生变化。所以让它成为 subView 而不是呈现 viewController。查看 viewController 编程指南。
  • 但是我有 100 多个不同的屏幕...我必须将它们全部设为子视图吗?必须有某种方法来解决这个问题......我仍然不明白为什么这在 iOS4 上工作,现在它不......
  • 当我使用此代码返回视图时是否调用了任何方法? [[self presentingViewController]dismissViewControllerAnimated:YES 完成:nil];

标签: iphone ios5 uiscrollview xcode3to4


【解决方案1】:

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

[self.view addSubview:anotherViewController.view];

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

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

[self presentModalViewController:anotherViewController animated:YES];

因此,您需要在父级和模态视图本身中启用 shouldAutorotateToInterfaceOrientation。

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

【讨论】:

  • 我尝试使用 addsubview 和 removesubview 但不起作用...添加的视图无法对方向更改做出正确反应。如果我在横向上添加一个,它会被添加,但它会出现在纵向上。所以,我可以在后面看到横向的另一个视图,以及前面添加的垂直视图(但设备是横向的)。
  • 尝试一个只有两个视图和所有可能组合的新项目,它应该可以工作......
  • 我想我已经修好了。我明天会发布解决方案(我现在不能这样做,我必须等待 8 小时,论坛说:P)。感谢您的建议,我会记住下一个项目的!
  • 如您所说,当我离开“孩子”视图时,我只是向通知中心发送通知。 “父”视图是观察者,当它接收到信号时,它会运行 shouldAutorotateToInterfaceOrientation 方法来正确实现视图。我明天会发布代码:D
【解决方案2】:

在 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 方法。请记住,在此方法中,我会调整所有内容的大小以适应应用程序的当前方向。

【讨论】:

    猜你喜欢
    • 2013-12-26
    • 1970-01-01
    • 2015-11-07
    • 2019-01-05
    • 2014-09-13
    • 2016-08-24
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多