這篇文章是關(guān)于在一個頁面內(nèi)的橫豎屏切換(網(wǎng)上找了一堆都不太適用,問了朋友加上自己修改結(jié)合的方法):
首先我的設(shè)備方向是固定豎屏的:
1.在AppDelegate.m里添加你需要支持的方向:
我這里支持了豎屏和home在右側(cè)的橫屏
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationMaskPortrait |UIInterfaceOrientationMaskLandscapeRight;
}
之所以不在General里直接勾選支持而在AppDelegate.m里添加,是可以避免掉launchscreen.storyboard橫屏,如果你的storyboard支持橫屏的話,可以不添加上面的代碼,直接在General里添加支持就可以
2.在需要切換屏幕的頁面添加支持:
- (BOOL)shouldAutorotate
{
return YES;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight |UIInterfaceOrientationMaskPortrait;
}
3.調(diào)用橫屏代碼
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 = UIInterfaceOrientationLandscapeRight;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
[UIViewController attemptRotationToDeviceOrientation];
}
后續(xù)發(fā)現(xiàn)在某些真機的時候未生效,分析應(yīng)該是Nav和Tabbar的支持沒有寫入,放一下我自己的Nav和Tabbar代碼
- MainNav.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface MainNav : UINavigationController
@end
NS_ASSUME_NONNULL_END
2.MainNav.m
#import "MainNav.h"
@interface MainNav ()
@end
@implementation MainNav
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
//是否自動旋轉(zhuǎn),返回YES可以自動旋轉(zhuǎn)
- (BOOL)shouldAutorotate
{
if ([self.topViewController respondsToSelector:@selector(shouldAutorotate)])
{
return [self.topViewController shouldAutorotate];
}
return NO;
}
//返回支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
if ([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
{
return [self.topViewController supportedInterfaceOrientations];
}
return UIInterfaceOrientationMaskPortrait;
}
//這個是返回優(yōu)先方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
if ([self.topViewController respondsToSelector:@selector(preferredInterfaceOrientationForPresentation)])
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
return UIInterfaceOrientationPortrait;
}
@end
3.mainTabBar.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface MainTabBar : UITabBarController
@end
NS_ASSUME_NONNULL_END
4.MainTabBar.m
#import "MainTabBar.h"
#import "ShuMuVC.h"
#import "ZhiBoVC.h"
#import "TwoMineVC.h"
#import "HomePageG/HomeVC.h"
#import "MainNav.h"
@interface MainTabBar ()
@end
@implementation MainTabBar
- (void)viewDidLoad {
[super viewDidLoad];
HomeVC *homevc = [[HomeVC alloc]init];
MainNav *homeNav = [[MainNav alloc]initWithRootViewController:homevc];
homeNav.tabBarItem.image = [[UIImage imageNamed:@"tabbar_home"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
homeNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_home_s"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
homeNav.title = @"首 頁";
ZhiBoVC *yunsjvc = [[ZhiBoVC alloc]init];
MainNav *yunsjNav = [[MainNav alloc]initWithRootViewController:yunsjvc];
yunsjNav.tabBarItem.image = [[UIImage imageNamed:@"tabbar_live"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
yunsjNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_live_s"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
yunsjNav.title = @"視 頻";
//CaiFVC *caifuvc = [[CaiFVC alloc]init];
ShuMuVC *caifuvc = [[ShuMuVC alloc]init];
MainNav *caifuNav = [[MainNav alloc]initWithRootViewController:caifuvc];
caifuNav.tabBarItem.image = [[UIImage imageNamed:@"tabbar_book"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
caifuNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_book_s"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
caifuNav.title = @"書 目";
TwoMineVC *minevc = [[TwoMineVC alloc]init];
MainNav *mineNav = [[MainNav alloc]initWithRootViewController:minevc];
mineNav.tabBarItem.image = [[UIImage imageNamed:@"tabbar_mine"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
mineNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_mine_s"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
mineNav.title = @"我 的";
NSArray *navary = @[homeNav,yunsjNav,caifuNav,mineNav];
self.viewControllers = navary;
}
- (BOOL)shouldAutorotate
{
MainNav *nav = (MainNav *)self.selectedViewController;
if ([nav isKindOfClass:[MainNav class]])
{
return [self.selectedViewController shouldAutorotate];
}
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
MainNav *nav = (MainNav *)self.selectedViewController;
if ([nav isKindOfClass:[MainNav class]])
{
return [self.selectedViewController supportedInterfaceOrientations];
}
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
MainNav *nav = (MainNav *)self.selectedViewController;
if ([nav isKindOfClass:[MainNav class]])
{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
return UIInterfaceOrientationPortrait;
}
@end
TabBar里換成自己的頁面就可以了.