iOS 屏幕横竖屏转换的方法

本文介绍了在iOS开发中实现屏幕横竖屏切换的步骤,包括修改Info.plist文件、在AppDelegate中添加属性方法以及在特定控制器中实现旋转功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 首先选择支持的旋转方向(两种方法,推荐第二种)
    (1)修改Info.plist文件,见图1                                                                                                                         iOS <wbr>屏幕横竖屏转换的方法

    (2)直接上图,(勾选即可)
    iOS <wbr>屏幕横竖屏转换的方法

    2.在AppDelegate中添加属性方法
    在.h中添加一个属性allowRotation

    @interface AppDelegate : UIResponder 
    
    @property (strong, nonatomic) UIWindow *window;
    
    @property(nonatomic,assign)BOOL allowRotation;//是否允许转向
    
    @end
    

    .m中添加下面的方法

    - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window{
        if (_allowRotation == YES) {
            return UIInterfaceOrientationMaskLandscapeRight;
        }else{
            return (UIInterfaceOrientationMaskPortrait);
        }
    }
    

    3.在你需要旋转的控制器.m中添加一下方法

    - (void)setNewOrientation:(BOOL)fullscreen{
        if (fullscreen) {
            NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
            [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
    
            NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
            [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
        }else{
            NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
            [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
    
            NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
            [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
        }
    }
    

    4.点击旋转按钮调用- (void)setNewOrientation:(BOOL)fullscreen方法

    //横竖屏切换按钮方法
    -(void)screen{
        //记着#import "AppDelegate.h"
        AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
        if (_fullScreen ) {//横屏情况下,点击此按钮变为竖屏
            appDelegate.allowRotation = NO;//设置竖屏
            [self setNewOrientation:NO];//调用转屏代码
            self.navigationController.navigationBar.hidden = NO;//navbar消失
            [self setViewFrame:NO];//竖屏布局
        }else{//竖屏情况下,点击此按钮变为横屏
            appDelegate.allowRotation = YES;设置横屏
            [self setNewOrientation:YES];调用转屏代码
            self.navigationController.navigationBar.hidden = YES;//navbar出现
            [self setViewFrame:YES];//横屏布局
        }
    }     
    
    3.获取当前屏幕是竖屏还是横屏          
    
    #import "AppDelegate.h"
    
    
    @interface JGCameraViewController ()
    @property (nonatomic, assign) UIInterfaceOrientation interfaceOrientation;
    @end
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];     //改变AppDelegateappdelegete.allowRotation属性     AppDelegate *appdelegete = (AppDelegate *)[UIApplication sharedApplication].delegate;     appdelegete.allowRotation = YES;     self.interfaceOrientation = 1;
    }
    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
        self.interfaceOrientation = interfaceOrientation;     switch (interfaceOrientation) {         case UIInterfaceOrientationPortrait:             //home健在下             break;
            case UIInterfaceOrientationPortraitUpsideDown:
                //home健在上

                break;
            case UIInterfaceOrientationLandscapeLeft:
                //home健在左
                break;
            case UIInterfaceOrientationLandscapeRight:             //home健在右
                break;
            default:
                break;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值