UINavigationController、UITabBarController 支持屏幕自动旋转问题

写了一篇关于屏幕强制旋转的文章,可是有人跟我反应说没成功,状态栏依旧没有改变。原因是他的rootViewController是UINavigationController跟UITabBarController混合使用的。

因为UINavigationController跟UITabBarController也是UIViewController,所以会覆盖其子页面的旋转方法。

解决方法:添加以下类

//
//  UINavigationController+Rotation_IOS6.m
//  djy
//
//  Created by SuFuTe on 14/12/30.
//  Copyright (c) 2014年 SuFuTe. All rights reserved.
//

#import "UINavigationController+Rotation_IOS6.h"

@implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate {
    
    NSLog(@"UINavigationController 100");
    // 不想其子页面支持旋转, 可直接返回 NO
    return YES;
}


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

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


//
//  UITabBarController+Rotation_IOS6.m
//  djy
//
//  Created by SuFuTe on 14/12/30.
//  Copyright (c) 2014年 SuFuTe. All rights reserved.
//

#import "UITabBarController+Rotation_IOS6.h"

@implementation UITabBarController (Rotation_IOS6)
-(BOOL)shouldAutorotate {
    NSLog(@"UITabBarController 100");
    // 不想其子页面支持旋转,可直接返回 NO
    return YES;
}
-(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


AppDelegate.m中导入头文件

#import "UINavigationController+Rotation_IOS6.h"
#import "UITabBarController+Rotation_IOS6.h"


在想要旋转的页面重写以下方法,设置你想要的旋转方向

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值