保持视图在导航控制器中的所有视图后面持久化的最佳方法

时间:2010-11-14 02:34:00

标签: iphone uinavigationcontroller background

并提前致谢。

我正在寻找有关如何在我的应用中布局视图控制器的建议,其中我仍然有持久的背景以及一些背景动画

目前的设置方式如下:

  • AppDelegate制作导航控制器,RootViewController和Sprite Layer(子类UIView)
  • AppDelegate还将背景图像保存为backgroundColor属性
  • 导航控制器使用根视图控制器进行初始化,正常情况下
  • rootview控制器将不同的表视图推送到导航堆栈

在代码中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

UIImageView* backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"underthesea.jpg"]] autorelease];
backgroundView.contentMode = UIViewContentModeScaleAspectFill;
backgroundView.frame = [UIScreen mainScreen].bounds;

self.viewController = [[[RootViewController alloc] init] autorelease];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.spriteLayer = [[[SpriteLayer alloc] initWithFrame:self.viewController.view.frame] autorelease];
self.viewController.spriteLayer = self.spriteLayer;

[window addSubview:backgroundView];    
[window addSubview:self.spriteLayer];
[window addSubview:self.navigationController.view];

[window makeKeyAndVisible];
return YES;
}

`

所有时间,Sprite Layer在后台都可见,包含不同的动画。问题是,现在我正在尝试实现autorisizing并遇到困难,因为sprite层中的视图不在UIViewController中。

我认为一种可能性是使SpriteLayer子类为UIViewController,但不要将它放在导航控制器中;它只会存在于导航控制器正在显示的任何视图后面。我不知道是否同时拥有2个视图控制器,这将是错误的来源或只是糟糕的设计,因为我在StackOverflow上读到这不是视图控制器的预期设计。

这里有什么意见吗?

干杯, 富

P.S。我在代码块中放置一个客观的C方法时遇到问题。后面的勾号似乎不起作用,因为代码字体中唯一的代码部分是缩进的。有人知道这样做的正确方法吗?再次感谢。

2 个答案:

答案 0 :(得分:5)

Bentford,我没想过将SpriteLayer视图直接添加到UINavigation视图中,因为我认为这不是UINavigation的预期用途。但它的作用就像一个魅力:)

我必须改变的是:

[window addSubview:backgroundView];    
[window addSubview:self.spriteLayer];
[window addSubview:self.navigationController.view];

为:

[self.navigationController.view insertSubview:backgroundView atIndex:0];
[self.navigationController.view insertSubview:self.spriteLayer atIndex:1];
[window addSubview:self.navigationController.view];

然后在backgroundView和poof中添加一点autoresizeMask我有一个自动调整背景+精灵,当视图控制器被推入导航堆栈时,它没有git滑出。

感谢您的建议!

答案 1 :(得分:0)

不要使用多个视图控制器。相反,让SpriteLayer子类UIView并将其直接添加到当前可见的UIViewController视图中。

请记住,只有可见的UIViewController会触发旋转事件。但是通过这种方式设置SpriteLayer,您可以使用自动调整大小或在didRotateToInterfaceOrientation中手动调整大小。

只需将SpriteLayer添加到view属性即可将其添加到您的UINavigationController中。一个好方法是通过继承UINavigationController并覆盖viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];

    SpriteLayer *spriteLayer = [[SpriteLayer alloc] initWithFrame:CGRectMake(0, 300, 320, 50)];
    [self.view addSubview:spriteLayer];

}

推送和弹出动画将在SpriteLayer后面运行。使用sendSubviewToBack和相关的UIView方法可以实现其他行为。