美文网首页
iOS 单屏幕横屏

iOS 单屏幕横屏

作者: 索性流年 | 来源:发表于2018-06-25 11:03 被阅读0次

在AppDelegate.h中建立属性,用于配置横竖屏

//设置允许转屏
@property (nonatomic, assign) NSString * isScreenDirection;

之后AppDelegate.m中

#pragma mark - 转屏设置
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    //判断如果isScreenDirection等于YES将允许横竖屏切换
    if ([self.isScreenDirection isEqualToString:@"全部"]) {
        return UIInterfaceOrientationMaskAll;//适应所有方向
    }
    else if ([self.isScreenDirection isEqualToString:@"横屏"]){
        return UIInterfaceOrientationMaskLandscape;//横屏
    }
    else{
        return UIInterfaceOrientationMaskPortrait;//竖屏
    }
} 

在需要设置横屏的地方设置

#import "AppDelegate.h"
-(void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
//将标示改为YES此标记为允许横屏标记
  AppDelegate * app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  app.isScreenDirection = @"全部";
}

监听屏幕方向

[[NSNotificationCenter     defaultCenter]addObserverForName:UIApplicationDidChangeStatusBarOrientationNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication]statusBarOrientation];
    if (interfaceOrientation == UIInterfaceOrientationPortrait) {
        //竖屏
    }
    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        //横屏
    }
  //其他自己写去
}];

相关文章

网友评论

      本文标题:iOS 单屏幕横屏

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