【问题标题】:How to solve orientation issue in iOS 6 with UITabBarController use?如何使用 UITabBarController 解决 iOS 6 中的方向问题?
【发布时间】:2012-10-10 00:43:41
【问题描述】:

在我的应用程序中,我有一个横向视图控制器,只要设备处于可正常工作的横向模式,就应该调用它。但问题是当我旋转回纵向时,它就搞砸了。 我在我的 App 委托中这样做:

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

    [NSThread sleepForTimeInterval:1.5];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);


    // Override point for customization after application launch.
    UIViewController *viewController1, *viewController2, *viewController3, *viewController4, *viewController5;

    UIImage * logoImage = [UIImage imageNamed:NAVIGATION_LOGO_IMAGE];
    self.navController.navigationItem.titleView = [[UIImageView alloc] initWithImage:logoImage];
    self.navController.navigationItem.titleView.frame = CGRectMake(340,0 , 450, 55);

    // Create initialized instance of UITabBarController
    tabController = [[UITabBarController alloc] init];
    NSMutableArray *tabs = [[NSMutableArray alloc] init];
    // Create first UIViewController
    viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil];
    [viewController1 setTitle:@"Home"];
    navController = [[UINavigationController alloc] initWithRootViewController:viewController1];
    navController.navigationBar.tintColor = [UIColor blackColor];
    [tabs addObject:navController];

   viewController2 = [[TwitterDataLoad alloc] initWithNibName:@"TwitterDataLoad" bundle:nil];
     //viewController2 = [[TwitterDataController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil];
    [viewController2 setTitle:@"News"];
    navController = [[UINavigationController alloc] initWithRootViewController:viewController2];
     navController.navigationBar.tintColor = [UIColor blackColor];
    [tabs addObject:navController];

    viewController3 = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    [viewController3 setTitle:@"Music"];
    navController = [[UINavigationController alloc] initWithRootViewController:viewController3];
     navController.navigationBar.tintColor = [UIColor blackColor];
    [tabs addObject:navController];

    viewController4 = [[ThumbNailViewController alloc] initWithNibName:@"ThumbNailViewController" bundle:nil];
    [viewController4 setTitle:@"Gallery"];
    navController = [[UINavigationController alloc] initWithRootViewController:viewController4];
     navController.navigationBar.tintColor = [UIColor blackColor];
    [tabs addObject:navController];

    viewController5 = [[FifthViewController alloc] initWithNibName:@"FifthViewController" bundle:nil];
    [viewController5 setTitle:@"Shows"];
    navController = [[UINavigationController alloc] initWithRootViewController:viewController5];
     navController.navigationBar.tintColor = [UIColor blackColor];
    [tabs addObject:navController];

    landscapeVC = [[LandscapeAboutViewController alloc] initWithNibName:@"LandscapeAboutViewController" bundle:nil];
    infamous = [[InfamousViewController alloc]initWithNibName:@"InfamousViewController" bundle:nil];
    NSArray *versionCompatibility = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];

    if ( 5 == [[versionCompatibility objectAtIndex:0] intValue] ) 
    {
        [[UITabBar appearance]setTintColor:[UIColor blackColor]];
        [[UITabBar appearance]setSelectedImageTintColor:[UIColor whiteColor]];
    }
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSString * popUpShown = [defaults objectForKey:@"PopUpShown"];

    if (![popUpShown isEqualToString:@"yes"]) {

        [self showAlertView];
    }

    [tabController setViewControllers:tabs];

    self.window.rootViewController = tabController;
    [self.window makeKeyAndVisible];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];


    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didRotate:)
                                                 name:UIDeviceOrientationDidChangeNotification object:nil];

    return YES;
}

- (void) didRotate:(NSNotification *)notification
{   
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if (UIDeviceOrientationIsLandscape(orientation)) {
        if (self.window.rootViewController != landscapeVC) {
            [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
            //[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationIsLandscape(orientation)];
            [self switchToLandScape];

        }

    }
    else if (UIDeviceOrientationIsPortrait(orientation)){
        if (self.window.rootViewController != tabController) {
            [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
            [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationIsPortrait(orientation)];

            [self switchToTabBar];

        }
    }
}

- (void)switchToTabBar {

    self.window.rootViewController = tabController;
}

- (void)switchToLandScape {

    self.window.rootViewController = landscapeVC;

}

【问题讨论】:

    标签: objective-c uitabbarcontroller ios6


    【解决方案1】:

    尝试继承 UITabBarController 并在该子类中实现新的 iOS 6 旋转委托方法。然后将该子类用作您的根视图控制器,以便在 iOS 6 下更好地控制旋转。

    【讨论】:

      【解决方案2】:

      在您的标签栏控制器子类中指定:

      - (NSUInteger)supportedInterfaceOrientations{
          return UIInterfaceOrientationMaskLandscape;
      }
      

      另一种选择是将其放置在导航控制器子类中,然后将特定于景观的视图控制器嵌入到该导航控制器中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-30
        • 1970-01-01
        • 2012-12-26
        • 1970-01-01
        • 2012-09-12
        相关资源
        最近更新 更多