1. 程式人生 > >ios6.0 UINavigationController、UITabBarController 支援螢幕自動旋轉問題

ios6.0 UINavigationController、UITabBarController 支援螢幕自動旋轉問題

1、描述

      self.window.rootViewController = UINavigationController 或 UITabBarController。

或者是混合使用的,如: UINavigationController 下包含UITabBarController

     因  UINavigationController、UITabBarController 也是 UIViewController,所以會覆蓋其子頁面的旋轉方法

2、解決,新增類

@interface UINavigationController (Rotation_IOS6)

-(BOOL)shouldAutorotate;
-(NSUInteger)supportedInterfaceOrientations;
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;

@end

#import "UINavigationController+Rotation_IOS6.h"

@implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate {
    
    NSLog(@"UINavigationController 100");
    // 不想其子頁面支援旋轉, 可直接返回 NO
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations {
    NSLog(@"UINavigationController 200");
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    NSLog(@"UINavigationController 300");
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end

@interface UITabBarController (Rotation_IOS6)

-(BOOL)shouldAutorotate;
-(NSUInteger)supportedInterfaceOrientations;
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;

@end

@implementation UITabBarController (Rotation_IOS6)

-(BOOL)shouldAutorotate {
    NSLog(@"UITabBarController 100");
    // 不想其子頁面支援旋轉,可直接返回 NO
    return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations {
     NSLog(@"UITabBarController 200"); 
    //return UIInterfaceOrientationMaskPortrait;
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { 
    NSLog(@"UITabBarController 300"); //return UIInterfaceOrientationPortrait;
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}@end

3、AppDelegate.m 中 使用
#import "UINavigationController+Rotation_IOS6.h"
#import "UITabBarController+Rotation_IOS6.h"

4、自定義的 UIViewController 中再設定對應的旋轉方法
- (BOOL)shouldAutorotate {
- (NSUInteger)supportedInterfaceOrientations{
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{

5、ios5 支援螢幕旋轉

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  
    return (interfaceOrientation == UIInterfaceOrientationPortrait);  
}