iOS界面横屏竖屏随意切换

本文详细介绍如何在iOS应用中实现特定界面的横竖屏切换功能。通过在AppDelegate中定义Rotate属性并重写相关方法,可以灵活控制界面的旋转状态。同时,提供了手动和自动切换的实现方式,包括利用button点击事件和viewWillAppear/viewWillDisappear方法来调整屏幕方向。

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

 

 APP中所有界面支持竖屏,只有在一个界面,支持横竖屏

横屏竖屏转换的时候,屏幕的大小和控件的尺寸发生了变化,应采用Masonry自动布局的方法

 

手动切换,点击按钮

1. 在AppDelegate中定义一个Rotate,用于记录横竖屏

       @property (nonatomic, assign) NSInteger Rotate;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

               self.Rotate = 0;

}

//当设备横竖屏转换的时候回调用这个方法

- (UIInterfaceOrientationMask) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

{

            //为1的话,说明某个界面需要横竖屏转化

              if (_Rotate == 1) {

                   return UIInterfaceOrientationMaskAll;

              }else {

                 //为0的话,说明只需要竖屏

              return (UIInterfaceOrientationMaskPortrait);

    }

}

// 是否支持设备自动旋转

- (BOOL)shouldAutorotate

{

    if (_Rotate == 1) {

        //为1的话,支持旋转

        return YES;

    }

    return NO;

}

2. 哪个界面要实现此功能,就在哪个界面上

例如自定义一个button,在button的点击事件中做处理

- (void)buttonCilck:(UIButton *)sender
{
    sender.selected =! sender.selected;
    
    if(sender.selected)
    {
        NSlog(@"横屏");
        
        AppDelegate * appdelegate =(AppDelegate *) [UIApplication  sharedApplication].delegate;
        appdelegate.Rotate = 1;
        
        //如果强制横屏,打开这段代码,并且将手机设备的竖排方向锁定打开即可
        
        if ([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
            [self orientationToPortrait:UIInterfaceOrientationLandscapeRight];
        }
    }
    else
    {
        NSlog(@"竖屏");
        
        AppDelegate * appdelegate = (AppDelegate *)[UIApplication  sharedApplication].delegate;
        appdelegate.Rotate = 0;
        
        if ([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
            [self orientationToPortrait:UIInterfaceOrientationPortrait];
        }
    }
}

- (void)orientationToPortrait:(UIInterfaceOrientation)orientation
{
    SEL selector = NSSelectorFromString(@"setOrientation:");
    NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
    [invocation setSelector:selector];
    [invocation setTarget:[UIDevice currentDevice]];
    int val = orientation;
    [invocation setArgument:&val atIndex:2];
    [invocation invoke];
    
}

 

自动切换,push或present界面就是横屏,返回之后是竖屏

1. 方法和上面的方法1一样

2. 在具体的界面实现

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

     AppDelegate * appdelegate =(AppDelegate *) [UIApplication  sharedApplication].delegate;
        appdelegate.Rotate = 1;
        
        //如果强制横屏,打开这段代码,,并且将手机设备的竖排方向锁定打开即可 
        if ([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
            [self orientationToPortrait:UIInterfaceOrientationLandscapeRight];
}


- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

       AppDelegate * appdelegate = (AppDelegate *)[UIApplication  sharedApplication].delegate;
        appdelegate.Rotate = 0;
        
        if ([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
            [self orientationToPortrait:UIInterfaceOrientationPortrait];
        }
}

- (void)orientationToPortrait:(UIInterfaceOrientation)orientation
{
    SEL selector = NSSelectorFromString(@"setOrientation:");
    NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
    [invocation setSelector:selector];
    [invocation setTarget:[UIDevice currentDevice]];
    int val = orientation;
    [invocation setArgument:&val atIndex:2];
    [invocation invoke];
    
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值