美文网首页iOS经典
iOS开发之强制横竖屏

iOS开发之强制横竖屏

作者: KODIE | 来源:发表于2018-01-11 16:29 被阅读183次

步骤和代码

  • 第一步 :Appdelegate文件中设置

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

//在AppDelegate.h文件中设置两个属性
@property(nonatomic, assign) BOOL isForcePortrait;
@property(nonatomic, assign) BOOL isForceLandscape;

@end
//在AppDelegate.m文件中实现代理方法
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    if (self.isForceLandscape) {
        return UIInterfaceOrientationMaskLandscape;
    }else if (self.isForcePortrait){
        return UIInterfaceOrientationMaskPortrait;
    }else{
        return UIInterfaceOrientationMaskAll;
    }
}
  • 第二步:设置横竖屏的方法,写在分类中
//UIDevice+TFDevice.h文件
#import <UIKit/UIKit.h>

@interface UIDevice (TFDevice)
//interfaceOrientation 输入要强制转屏的方向
+ (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation;
@end
//UIDevice+TFDevice.m文件
#import "UIDevice+TFDevice.h"

@implementation UIDevice (TFDevice)

+ (void)switchNewOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
    [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
    NSNumber *orientationTarget = [NSNumber numberWithInt:interfaceOrientation];
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
@end
  • 第三步:设置横竖屏
#pragma  mark - 强制横竖屏设置
- (void)forceOrientationLandscape { //强制横屏
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.isForcePortrait = NO;
    appDelegate.isForceLandscape = YES;
    [UIDevice switchNewOrientation:UIInterfaceOrientationLandscapeRight];
}
- (void)forceOrientationPortrait  { //强制竖屏
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.isForcePortrait = YES;
    appDelegate.isForceLandscape = NO;
    [UIDevice switchNewOrientation:UIInterfaceOrientationPortrait];
}

注意

  • 这个在展示控制器的时候需要调用,在dismiss或者pop掉控制器的时候也需要将设置恢复原样。

  • 在第三步上强制横屏的以下这句代码中,后面的旋转方向仅仅是告诉屏幕要往横屏右方向转,至于转完之后屏幕能不能也能横屏左那就要看Appdelegate里面实现的代理方法怎么实现的了...

 [UIDevice switchNewOrientation:UIInterfaceOrientationLandscapeRight];

DEMO传送门

参考

iOS强制横屏或强制竖屏

以上!!!

弹钢琴.gif

相关文章

  • iOS开发之强制横竖屏

    步骤和代码 第一步 :Appdelegate文件中设置 第二步:设置横竖屏的方法,写在分类中 第三步:设置横竖屏 ...

  • IOS 横竖屏

    //iOS规定不允许强制用代码切换横竖屏 if([[UIDevicecurrentDevice]respondsT...

  • iOS 强制横竖屏

    最近在修改直播项目出现的问题,需要在直播页面强制进入横屏,记录下遇到的坑,稍后会写一篇阿里云直播的文章. 程序单独...

  • iOS 强制横竖屏

    强制横屏 强制竖屏 ⚠️ 在执行上述方法时要重写controller的一下方法

  • iOS 切换横竖屏

    参照:iOS强制转换横竖屏和键盘方向控制 实现点击按钮切换横竖屏的功能,设备锁屏无影响。效果如图所示: 然后,就是...

  • iOS 之强制横竖屏问题

    1、iOS开发中一般情况只会用到竖屏,但有些app 的个别页面却要变成横屏,所以在很竖屏的切换就是一个经常用到的问...

  • 关于iOS横竖屏适配

    关于iOS横竖屏适配 关于iOS横竖屏适配

  • iOS 横竖屏强制转换

    强制转成横屏:// 如果要上传AppStore请慎用,不知道是否能够通过,未尝试if ([[UIDevice cu...

  • iOS 屏幕旋转控制

    /** 屏幕旋转控制 allowRotateType ==(0强制竖屏,1横竖屏,2~强制横屏 )*/ (UIIn...

  • 2021-10-21

    uni-app强制横竖屏 //强制横屏 plus.screen.lockOrientation('landscap...

网友评论

    本文标题:iOS开发之强制横竖屏

    本文链接:https://www.haomeiwen.com/subject/xxhtoxtx.html