不同根视图下控制部分屏幕旋转(tabbarController/navigationController)

部分转自:http://blog.csdn.net/ioswyl88219/article/details/24711197


系统支持横屏顺序
默认读取plist里面设置的方向(优先级最高)等同于Xcode Geneal设置里面勾选
application window设置的级别次之
然后是UINavigationcontroller
级别最低的是viewcontroller

以下三条式子为控制屏幕翻转的核心代码

- (BOOL)shouldAutorotate//是否支持旋转屏幕{
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations//支持哪些方向{
    return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation//默认显示的方向{
    return UIInterfaceOrientationPortrait;
}
只要在plist或者General设置了单一方向,在工程里面的任何代码都会失效。所以想要旋转屏幕,必须让你的工程支持所旋转的方向。(但是这里还有一种方式是把当前控制器的View或者view.layer做transform旋转,但是我认为这并不是真正的旋转屏幕,一点可以证明的就是:状态条并不随着旋转)
其实这个优先级跟你的window的rootViewcontroller有关系,如果rootViewcontroller是UITabbarViewontroller,那么tabbar就类似前面所说的UINavigationcontroller,在iOS6之后好多方法发生了改变,之前的一个屏幕旋转的方法就失效了,更改位上面的方法。
其实屏幕旋转在手机里面有设置,上拉菜单就会有关闭屏幕旋转的方式,但是大家都知道好多应用在关闭屏幕旋转的时候也可以进行屏幕旋转---通过某一个按钮的事件来触发,这就是强制旋转(前提依然是你的工程设置支持你所要旋转的方向)



屏幕设置为

在info.plist文件中将 View controller-based status bar appearance 设置为NOapplication:didFinishLaunchingWithOptions:中添加下面代码

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

如此之后你就会发现消失的状态条有乖乖的回来了。



设置屏幕旋转对应的是根视图控制器 -(BOOL)shouldAutotate方法只在根视图控制器走一次 所以只有在根视图设置有效,后面push的视图控制器也对应根视图设置的旋转方法 

设置部分屏幕旋转:

根视图控制器为NavigationController
创建继承NAV的类
- (BOOL)shouldAutorotate {  

//允许选择的视图控制器
    if ([NSStringFromClass([self.visibleViewController class]) isEqualToString:@"CCViewController"]) {  
        return YES;  
    }  
      
    return NO;  
}  
- (NSUInteger)supportedInterfaceOrientations {  
    return UIInterfaceOrientationMaskAllButUpsideDown;  
}  



参考: http://stackoverflow.com/questions/12522903/uitabbarcontroller-rotation-issues-in-ios-6
根视图控制器为TabBarController
创建NavigationController和TabBarVontroller的category
这里注意不能在tabbarcontroller的类里写旋转方法 否则以下方法会无效

#import UITabBarController+autoRotate.h

#import <UIKit/UIKit.h>

@interface UITabBarController (autoRotate)
-(BOOL)shouldAutorotate;
-(NSUInteger)supportedInterfaceOrientations;
@end

#import "UITabBarController+autoRotate.h"

@implementation UITabBarController (autoRotate)
-(BOOL)shouldAutorotate{
 
    return [self.selectedViewController shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations{

    return [self.selectedViewController supportedInterfaceOrientations];
}
@end


#import UINavigationController+autoRotate.h
@interface UINavigationController (autoRotate)
-(BOOL)shouldAutorotate;
-(NSUInteger)supportedInterfaceOrientations;
@end

在这里写上需要部分允许旋转的视图控制器
-(BOOL)shouldAutorotate{
    if ([NSStringFromClass([self.visibleViewController class])isEqualToString:@"PicShowViewController"]) {
        return YES;
    }
//    return [self.visibleViewController shouldAutorotate];
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations{

    return [self.visibleViewController supportedInterfaceOrientations];
}


监听设备旋转
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onDeviceOrientationChange) name:UIDeviceOrientationDidChangeNotification object:nil];

-(void)onDeviceOrientationChange
{
 
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(orientation)) {
            [self.view setNeedsLayout];
            [self.view layoutIfNeeded];
    }else if (orientation==UIDeviceOrientationPortrait){
           [self.collectionView reloadData];
            [self.view setNeedsLayout];
            [self.view layoutIfNeeded];
 
    }
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值