横竖屏控制加入以下3个函数即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #pragma mark--禁止横屏 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return (toInterfaceOrientation == UIInterfaceOrientationPortrait); }
- (BOOL)shouldAutorotate { return NO; }
-(UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }
|
这个方法可以实现,需要在根视图(nav,tabbar)和vc里重写
1 2 3
| 如果在UIViewController上,则在UIViewController对应的.m文件中加入三个函数即可。 如果在UITabBarController上,则在UITabBarController对应的.m文件中加入三个函数即可。 如果在UINavigationController上,则在UINavigationController对应的.m文件中加入三个函数即可。
|
页面基于UITabBarController使用自定义UITabBarController中加入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #pragma mark - - orientation
- (BOOL)shouldAutorotate { return [self.selectedViewController shouldAutorotate]; }
- (UIInterfaceOrientationMask)supportedInterfaceOrientations { return [self.selectedViewController supportedInterfaceOrientations]; }
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [self.selectedViewController preferredInterfaceOrientationForPresentation]; }
|
基于UINavigationController代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @property (nonatomic , assign) UIInterfaceOrientation interfaceOrientation; @property (nonatomic , assign) UIInterfaceOrientationMask interfaceOrientationMask;
#pragma mark - - orientation
- (BOOL)shouldAutorotate { return YES; }
- (UIInterfaceOrientationMask)supportedInterfaceOrientations { return self.interfaceOrientationMask; }
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return self.interfaceOrientation; }
|
1 2 3 4 5 6 7 8 9 10 11 12
| - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; navi.interfaceOrientation = UIInterfaceOrientationLandscapeRight; navi.interfaceOrientationMask = UIInterfaceOrientationMaskLandscapeRight;
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"]; [UIViewController attemptRotationToDeviceOrientation]; }
|
2.部分页面需要全屏,其他页面禁止横屏
在appdelegate.h添加以下属性:
1 2
| /*** 是否允许横屏的标记 */ @property (nonatomic,assign)BOOL allowRotation;
|
appdelegate.m添加如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| - (void)addNotificationCenter { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startFullScreen) name:@"startFullScreen" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:@"endFullScreen" object:nil]; }
#pragma mark 进入全屏 -(void)startFullScreen { AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.allowRotation = YES; }
#pragma mark 退出横屏 -(void)endFullScreen { AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.allowRotation = NO;
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { SEL selector = NSSelectorFromString(@"setOrientation:"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:[UIDevice currentDevice]]; int val =UIInterfaceOrientationPortrait; [invocation setArgument:&val atIndex:2]; [invocation invoke]; } }
#pragma mark 禁止横屏 - (UIInterfaceOrientationMask )application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if (self.allowRotation) { return UIInterfaceOrientationMaskAll; } return UIInterfaceOrientationMaskPortrait; }
|
上面通过通知修改allowRotation属性来控制横竖屏幕
当然也可以通过AppDelegate单例直接修改allowRotation以控制横竖屏
1 2
| AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.allowRotation = YES;
|
在viewWillAppear和viewWillDisappear分别调用以上方法,在该控制器下即可顺利支持全屏。