iOS强制横竖屏

因为公司项目要兼顾老版本的原因,有的需要横屏显示有的需要竖屏显示,这就带来很麻烦的事了,测试了几种方法,终于搞定,直接铺代码!

一、UINavigationController 设置

 

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation ==UIInterfaceOrientationLandscapeRight);
}
- (BOOL)shouldAutorotate
{

    return self.topViewController.shouldAutorotate;;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    returnself.topViewController.supportedInterfaceOrientations;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return self.topViewController.preferredInterfaceOrientationForPresentation;
}

   在需要横屏的VC里设置

  -(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self forceOrientationLandscape];

}

/**
 Description 强行横屏设置
 */
- (void)forceOrientationLandscape
{
    AppDelegate *appdelegate=(AppDelegate *)[UIApplicationsharedApplication].delegate;
    appdelegate.EDForceLandscapeRight = YES;
    
    [appdelegate application:[UIApplication sharedApplication]supportedInterfaceOrientationsForWindow:self.view.window];
}


/**
 Description  强制翻转屏幕
 
 @param Orientation 方向
 */
- (void)mandatoryDeviceOrientation:(UIInterfaceOrientation)Orientation
{
    [[UIDevice currentDevice] setValue:@([selfpreferredInterfaceOrientationForPresentation])forKey:@"orientation"];
}

/**
 Description  刷新设备方向
 */
- (void)refreshRotationToDeviceOrientation
{
     [UIViewController attemptRotationToDeviceOrientation];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (BOOL)shouldAutorotate
{
    return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;//只支持正常方向
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

在需要竖屏的VC里设置

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self forceOrientationPortrait];

    [self mandatoryDeviceOrientation:[self preferredInterfaceOrientationForPresentation]];
}

/**
 Description  强制竖屏
 */
- (void)forceOrientationPortrait
{
    AppDelegate *appdelegate=(AppDelegate *)[UIApplicationsharedApplication].delegate;
    
    appdelegate.EDForceLandscapeRight=false;
    [appdelegate application:[UIApplication sharedApplication]supportedInterfaceOrientationsForWindow:self.view.window];
}

/**
 Description  强制翻转屏幕
 
 @param Orientation 方向
 */
- (void)mandatoryDeviceOrientation:(UIInterfaceOrientation)Orientation
{
    [[UIDevice currentDevice] setValue:@([selfpreferredInterfaceOrientationForPresentation])forKey:@"orientation"];
}

/**
 Description  刷新设备方向
 */
- (void)refreshRotationToDeviceOrientation
{
     [UIViewController attemptRotationToDeviceOrientation];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

 

Appdelegate中设置

-(UIInterfaceOrientationMask)application:(UIApplication *)applicationsupportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (self.EDForceLandscapeRight)
    {
        returnUIInterfaceOrientationMaskLandscapeRight;
    }
    return UIInterfaceOrientationMaskPortrait;
}


info 中设置

测试效果

 


网上很多介绍下面的方法

-(void)interfaceOrientation:(UIInterfaceOrientation)orientation{ 

          if([[UIDevicecurrentDevice]respondsToSelector:@selector(setOrientation:)])

         {              

                    SEL selector =NSSelectorFromString(@"setOrientation:");     

                     NSInvocation*invocation =[NSInvocationinvocationWithMethodSignature:[UIDeviceinstanceMethodSignatureForSelector:selector]];        

                   [invocationsetSelector:selector];         

                    [invocation setTarget:[UIDevicecurrentDevice]];    

                    intval = orientation;     

                   // 从2开始是因为0 1 两个参数已经被selector和target占用   

                    [invocationsetArgument:&valatIndex:2];                                                       

                    [invocation invoke];     

        }

 }

这个方法并不实用,而且在切换过程中会有你意想不到的意外惊喜。

总结:1 在转屏过程中 方向枚举值要一致。

           2.调用顺序很重要

          3. 方法可提炼


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值