ios - 如果我使用 UINavigationController,内容不会旋转

标签 ios uinavigationcontroller screen-orientation

如果我在我的应用程序中使用 UINavigationController,应用程序内容不会旋转。只有状态栏方向改变。

这是我的 didFinishLaunchingWithOptions 方法。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.


    UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main"
                                                 bundle:nil];
    SpecificViewController *control=[sb instantiateViewControllerWithIdentifier:@"specific"];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:control];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [self.window setRootViewController:navigationController];

    return YES;
}

我不明白为什么我会得到这个结果。

enter image description here

找到它

最后,我找到了解决方案。但我不明白为什么。

这是我的 didFinishLaunchingWithOptions 方法。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    self.window.rootViewController =nil;
    UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main"
                                                 bundle:nil];
    SpecificViewController *control=[sb instantiateViewControllerWithIdentifier:@"specific"];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:control];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window setRootViewController:navigationController];

    return YES;
}

这是SpecificViewController 的viewDidLoad 方法。
- (void)viewDidLoad {
    [super viewDidLoad];

    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

    self.navigationItem.title=@"The Title";

}

我在 plist 文件中将“基于 Controller 的状态栏外观”属性设置为 NO。
它现在正在工作,但我不明白所有这些是什么意思以及为什么。

最佳答案

我不认为您接受的答案是正确的答案,我想如果它对您有用,那很好,但对我来说仍然是错误的。

旋转不起作用的原因是因为您使用的是自动创建 UIWindow 的 Storyboard。然后在您的应用程序 didFinishLaunching 方法中,您正在分配一个新的 UIWindow。只有第一个 UIWindow(由你的 Storyboard创建)会收到旋转通知,这就是为什么你只注意到状态栏旋转而不是你的 View 。

解决方案是从您的 didFinishLaunchingWithOptions 中删除以下行方法:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController];

您的 rootViewController 也将在 Storyboard 中设置,无需在代码中再次设置。

关于ios - 如果我使用 UINavigationController,内容不会旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26568443/

相关文章:

ios - uitableview + 导航 Controller 设置所选元素的标题

Android TV 强制应用程序处于纵向模式

ios - ipa 文件中的二进制文件到底是什么?

ios - 如何正确旋转 map 上的叠加层?

ios - 如何从外部 View Controller 转到标签栏项目

android - 关于安卓 :configChanges

android - 如何更改 Android 中 fragment 的方向?

ios - Cocoapods Post_Install 脚本错误

iOS:从文档目录获取文件名到 NSMutableArray

ios - UINavigationController 后退按钮标题不一致?