IOS横竖屏显示

由于IOS的横竖屏设置只有在根视图才会有效果。所以 想了一个方法就是找到根视图,添加一个类函数,在任何地方都去设置横竖屏的参数。

下边的例子是指定窗口横屏,其他竖屏显示。

1.找到根视图类,添加属性。我的demo工程根视图是 MyTabBarViewController 所以就在MyTabBarViewController中添加

static UIInterfaceOrientationMask orientation =UIInterfaceOrientationMaskPortrait;

添加类函数

+(void)setViewOrientation:(UIInterfaceOrientationMask)orientations{

 

    orientation = orientations;


}

重载函数

// 指定支持的旋转情况,须在最底层的viewcontroller实现

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

    returnorientation;

}


2.在要单独设置横屏的界面添加代码

 [MyTabBarViewControllersetViewOrientation:UIInterfaceOrientationMaskLandscapeRight |UIInterfaceOrientationMaskLandscapeLeft];

 由于设备当时可能是竖屏显示效果,如果没有手机方向改变的事件触发,就算是设置了横竖屏参数也不会横屏显示需要添加代码,去横屏显示

 

    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
    {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationLandscapeRight;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
  在该视图消失的时候,需要改回竖屏效果。同理需要添加代码

  

 if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
    {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
综上所述:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [MyTabBarViewController setViewOrientation:UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft];
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
    {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationLandscapeRight;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
    // Do any additional setup after loading the view.
}

-(void)viewWillDisappear:(BOOL)animated{

    [MyTabBarViewController setViewOrientation:UIInterfaceOrientationMaskPortrait];
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
    {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}


Demo源码







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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值