したがって、アプリケーションUIWindowに固定トップバナーを設定し、複数のビューコントローラの作業ナビゲーションを実現しようとしています。私はこれを実行する他の方法があることを知っていますが、私のプロジェクト要件ではxibsを使う必要があります。アプリケーションがアクティブになった後にUIWindowサブビューが消える
:私は、バナーの下に自分自身を示すためにself.viewとself.navigationController.navigationBar.frameの両方をリサイズした各ViewControllerをviewWillAppearメソッドに続いて- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.headerView = [[UIImageView alloc] init];
self.headerView.frame = CGRectMake(0.0f, 20.0f, 320.0f, 44.0f);
self.headerView.image = [UIImage imageNamed: @"header-banner-background.png"];
[self.window addSubview:self.headerView];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
:
次のコードは、これを達成することができます
self.navigationController.navigationBar.frame = CGRectMake(0, 64, 320, 44);
self.view.frame = CGRectMake(0, 64, 320, 396);
これは問題なく動作します。
しかし、ユーザーがホームボタンを押すと、アプリケーションが再びアクティブになったときに、最後のViewControllerがのが表示されますが、UIWindowバナーなし、およびself.viewとnavigationBarが一番上に表示されます見て、アクティブ辞任場合。
もちろん、現在のUIViewController viewWillAppearメソッドは、アプリケーションがアクティブを返すときには呼び出されませんが、別の関数にサイズ変更コードを置き、通知を通じて呼び出す場合でも機能しません。しかし、ユーザーが棒アイテムを押してナビゲーションスタック内を移動すると、すべてが再び機能し、UIWindowはheaderViewを表示します。
誰でも、アプリケーションがアクティブに戻ったときにUIWindowヘッダービューが消える理由を理解していますか?
EDIT 1:解決策はありますか?
@interface RootViewController()
{
ViewController *viewController;
UINavigationController *navigationController;
}
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
}
return self;
}
- (void)loadView
{
// Implement loadView to create a view hierarchy programmatically, without using a nib.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 88)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[imageView setImage:[UIImage imageNamed:@"header"]];
[view addSubview:imageView];
navigationController.navigationBar.frame = CGRectMake(0, 44, 320, 44);
[view addSubview:navigationController.navigationBar];
self.view = view;
}
EDIT 2:第二の可能な解決策、まだこれはviewController1のプッシュせず、罰金だ結果、ある
- (void)loadView
{
NSLog(@"loadView root");
// Implement loadView to create a view hierarchy programmatically, without using a nib.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 88)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[imageView setImage:[UIImage imageNamed:@"banner"]];
[view addSubview:imageView];
navigationController = [[UINavigationController alloc] init];
navigationController.navigationBar.frame = CGRectMake(0, 43, 320, 44);
[view addSubview:navigationController.navigationBar];
[self addChildViewController:navigationController];
self.view = view;
[self.view setBackgroundColor:[UIColor yellowColor]];
}
を働いていません(黄色の背景からであることに注意rootViewController):代わり
これをr追加のesult:
- (void)viewDidLoad
{
viewController1 = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil];
[navigationController pushViewController:viewController1 animated:YES];
}
新しいビューコントローラが押されていない(背景は黒であるべきである)と、ナビゲーションバーが消えました。
EDIT 3:Olloquiソリューション、まだ結果はviewController1が押されたが、何のナビゲーションバーが表示されていない、私は唯一のバナーを持っている
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setBackgroundColor:[UIColor redColor]];
UIView *bannerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.window addSubview:bannerView];
UIView *navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 416)];
ViewController1 *viewController1 = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController1];
self.navigationController.navigationBar.frame = CGRectMake(0, 0, 320, 44);
[navigationView addSubview:self.navigationController.navigationBar];
[self.window addSubview:navigationView];
self.window.rootViewController = self.navigationController;
self.window.autoresizesSubviews = YES;
[self.window makeKeyAndVisible];
return YES;
}
を働いていません。私は本当にこれを理解する必要があります、誰も異なるアプローチを持っていますか?
なぜXIB自体にあなたの意見にバナーを入れていませんか? uiviewcontrollerはそれを他のビューとともに管理します。 – onnoweb
@onnoweb、それが私が "固定"トップバナーを指定した理由です。私は、ナビゲーションがバナーに影響を与えたくないので、ユーザーが複数のView Controllerをナビゲートしている間は、バナーは静止していなければなりません。 –
これがiOS 5の場合のみ、 'load view'で' [self addChildViewController:navigationController] 'を呼び出す必要があります。 –