通过setOrientation:的办法强制性的旋转到一个特定的方向

本文介绍了一种在iOS应用中实现横屏显示的方法,避免直接调用设备方向设置API而被App Store拒绝。通过使用performSelector间接调用,并提供了一个手动设置屏幕旋转的示例,确保应用程序能够顺利通过审核。

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

转载自:https://www.cnblogs.com/zxykit/p/5157729.html

注意:Apple在3.0以后都不支持这个办法了,这个办法已经成为了私有的了,但是要跳过AppStroe的审核,需要一点巧妙的办法。

   不要直接调用[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]这样的办法来强制性的横屏,这样导致你的程序是很难通过AppStore审核的。但是你可以选择使用performSelector的办法来调用它。具体就几行代码如下:

//强制横屏
    if([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
       [[UIDevice currentDevice]performSelector:@selector(setOrientation:)
                                      withObject:(id)UIInterfaceOrientationLandscapeRight];
    }



在手动设置屏幕旋转的代码前,设置先将屏幕旋转至其他方向,再旋转至最终的方向。

#pragma mark ---- 设置屏幕方向

- (void)setNewOrientation:(BOOL)fullscreen


{

    if (fullscreen) {

        

        NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

        

        [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];

        

        NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];

        

        [[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"];

        

    }

    

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值