在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) {
//横屏
}
//其他自己写去
}];
网友评论