UITabBarController+UINavigationController中單個頁面橫屏(可push/present)步驟,希望可以幫到一些人。親測絕對有效。
1、target-general-deployment info-勾選想要的方向
2、代理中寫入一下代碼
AppDelegate.h
/**
是否橫屏
*/
@property (nonatomic,assign) BOOL orientationFlag;
AppDelegate.m
-(UIInterfaceOrientationMask)interfaceOrientationMask {
if (self.orientationFlag) {
return UIInterfaceOrientationMaskAll;
}else {
return UIInterfaceOrientationMaskPortrait;
}
}
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return [self interfaceOrientationMask];
}
// 返回是否支持设备自动旋转
- (BOOL)shouldAutorotate
{
if (self.orientationFlag) {
return YES;
}
return NO;
}
3、tabbarViewController公有類
BaseTabBarViewController.h
//能否旋轉
-(BOOL)shouldAutorotate
{
return self.selectedViewController.shouldAutorotate;
}
//允許方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [self.viewControllers[self.selectedIndex] supportedInterfaceOrientations];
}
// 默认方向
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.viewControllers[self.selectedIndex] preferredInterfaceOrientationForPresentation];
}
4、navigationViewController公有類
BaseNavigationViewController.h
//能否旋轉
-(BOOL)shouldAutorotate{
return self.topViewController.shouldAutorotate;
}
//允許方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return [self.topViewController supportedInterfaceOrientations];
}
// 默认方向
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
5、需要橫屏的控制器
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
((AppDelegate *)[UIApplication sharedApplication].delegate).orientationFlag = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
((AppDelegate *)[UIApplication sharedApplication].delegate).orientationFlag = NO;
}
//能否旋轉
- (BOOL)shouldAutorotate
{
return YES;
}
//允許方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
// 默认方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
6、橫屏控制器push到下一層豎屏的控制器
//能否旋轉
- (BOOL)shouldAutorotate
{
return YES;
}
//允許方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
// 默认方向
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait;
}
-(void)viewWillAppear:(BOOL)animated
{
//强制翻转屏幕
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
((AppDelegate *)[UIApplication sharedApplication].delegate).orientationFlag = NO;
}