iOS 横竖屏切换

1、AppDelegate中设置横竖屏开关

.h中

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property(nonatomic, assign) BOOL allowRotation;///允许自由转动屏幕

@property(nonatomic, assign) BOOL allowLandscape;///只允许横屏@end

.m中

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

if (self.allowRotation == 1) { 

      return UIInterfaceOrientationMaskAll;   

}    if (self.allowLandscape == 1) {       

return UIInterfaceOrientationMaskLandscape;   

}    return (UIInterfaceOrientationMaskPortrait);

}


2、相关运用

//允许横屏

AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate; delegate.allowLandscape = YES;

//强制横屏

NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown]; [[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];

NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight]; [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

3、界面返回时记得关闭横屏显示

AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate; delegate.allowLandscape = NO;

//返回时强制竖屏

NSNumber *reSetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown]; [[UIDevice currentDevice] setValue:reSetOrientationTarget forKey:@"orientation"];

NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

4、横竖屏代理(适配屏幕设置)

#pragma mark ------------切换横竖屏后位置更新

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{

[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

WWWeakSelf

[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {

///横屏后的界面frame:

CGSize screen = [UIScreen mainScreen].bounds.size;

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 

///width > height 则表示横屏显示

if (screen.width > screen.height) {

///相关处理

///  if (orientation == UIInterfaceOrientationLandscapeLeft) ///表示向左的横屏

}

} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { }];}

别忘了设置允许APP横竖屏哟


这样  横竖屏相关设置基本就完啦   关灯 下班~~~~~~~~~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • AppDelegate.h @property(nonatomic,assign)BOOL isAllowRota...
    Hanson_HSS阅读 1,436评论 0 0
  • 网上关于横竖屏切换的资料很多,但是很容易踩到坑,不是屏幕不旋转,就是屏幕旋转后没有状态栏等,在写的小demo里屏幕...
    凌云01阅读 3,552评论 0 0
  • 目录 一、最让人纠结的三种枚举 二、两种屏幕旋转的触发方式 三、屏幕旋转控制的优先级 四、开启屏幕旋转的全局权限 ...
    来闹的阅读 8,024评论 0 4
  • 上周四用了半天的时间,项目中写的有关视频横屏的问题都没有实现。查了很多资料,都没有解决。今天早上终于解决了。记录下...
    tiGress阅读 9,625评论 4 6
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 12,732评论 28 53