iPhone 旋转后 UINavigationBar 消失

发布于 2024-11-27 13:17:25 字数 2415 浏览 0 评论 0原文

我有一个应用程序在 didFinishLaunchingWithOptions 方法中设置 UINavigationController,如下所示:

 main_iPhone *mainiPhone = [[main_iPhone alloc] initWithNibName:@"main_iPhone" bundle:nil];
mainiPhone.navigationItem.title = @"TitleHere";

UIBarButtonItem *botaoSobre = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:mainiPhone action:@selector(goToAboutView)];
mainiPhone.navigationItem.rightBarButtonItem = botaoSobre;

navController = [[UINavigationController alloc] initWithRootViewController:mainiPhone];
navController.navigationBar.tintColor = [UIColor orangeColor];
navController.navigationBar.translucent = NO;

[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;

当我运行该应用程序时,导航栏仅在纵向模式下正确显示。但是,当我旋转设备以横向显示时,导航栏就会消失。当我旋转回纵向方式时,我也没有恢复导航栏。

PS:我的 mainiPhone.xib 文件有 2 个视图(一个在纵向模式下,一个在横向模式下)。我像这样切换 mainiPhone.m 内的视图:

- (void)willAnimateRotationToInterfaceOrientation:

(UIInterfaceOrientation) interfaceOrientation 持续时间: (NSTimeInterval) period {

if (interfaceOrientation == UIInterfaceOrientationPortrait) {
    self.view = self.portrait; 
    self.view.transform = CGAffineTransformIdentity;
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0)); 
    self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);


} else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
    self.view = self.landscape; 
    self.view.transform = CGAffineTransformIdentity; 
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90)); 
    self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);


} else if (interfaceOrientation ==  UIInterfaceOrientationPortraitUpsideDown) { 
    self.view = self.portrait; 
    self.view.transform = CGAffineTransformIdentity; 
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(180)); 
    self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);


} else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) { 

    self.view = self.landscape; 
    self.view.transform = CGAffineTransformIdentity; 
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90)); 
    self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);
}

}

另外,我的 shouldAutorotateToInterfaceOrientation 方法只返回 YES。

我做错了什么? 预先非常感谢您!

I have an app that sets the UINavigationController in the didFinishLaunchingWithOptions method like this:

 main_iPhone *mainiPhone = [[main_iPhone alloc] initWithNibName:@"main_iPhone" bundle:nil];
mainiPhone.navigationItem.title = @"TitleHere";

UIBarButtonItem *botaoSobre = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:mainiPhone action:@selector(goToAboutView)];
mainiPhone.navigationItem.rightBarButtonItem = botaoSobre;

navController = [[UINavigationController alloc] initWithRootViewController:mainiPhone];
navController.navigationBar.tintColor = [UIColor orangeColor];
navController.navigationBar.translucent = NO;

[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;

When I run the application, the navigation bar appears correctly only in portrait mode. However, when I rotate the device to landscape the NavigationBar disappears. When I rotate back to portrait way I also don`t get the NavigationBar back.

PS: My mainiPhone.xib file has 2 views (one in portrait mode and one in landscape mode). I switch the views inside the mainiPhone.m like this:

- (void)willAnimateRotationToInterfaceOrientation:

(UIInterfaceOrientation) interfaceOrientation duration:
(NSTimeInterval) duration {

if (interfaceOrientation == UIInterfaceOrientationPortrait) {
    self.view = self.portrait; 
    self.view.transform = CGAffineTransformIdentity;
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0)); 
    self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);


} else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
    self.view = self.landscape; 
    self.view.transform = CGAffineTransformIdentity; 
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90)); 
    self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);


} else if (interfaceOrientation ==  UIInterfaceOrientationPortraitUpsideDown) { 
    self.view = self.portrait; 
    self.view.transform = CGAffineTransformIdentity; 
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(180)); 
    self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);


} else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) { 

    self.view = self.landscape; 
    self.view.transform = CGAffineTransformIdentity; 
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90)); 
    self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);
}

}

Also, my shouldAutorotateToInterfaceOrientation method just returns YES.

What am I doing wrong?
Thank you very much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文