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

转载自: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"];

        

    }

    

}



猜你喜欢

转载自blog.csdn.net/lxlzy/article/details/79198745