1、引入Launch Screen Storyboard布局文件
2、如果需要获得安全页面展示(留出刘海区域,防止刘海遮挡)
RootViewController.mm添加viewSafeAreaInsetsDidChange函数
- (void)viewSafeAreaInsetsDidChange {
[super viewSafeAreaInsetsDidChange];
NSLog(@"viewSafeAreaInsetsDidChange %@",NSStringFromUIEdgeInsets(self.view.safeAreaInsets));
[self updateOrientation];
}
bool changeViewFrameV = false;
- (void)updateOrientation {
if (@available(iOS 11.0, *))
{
CGRect rect = [[UIScreen mainScreen]bounds];
CGSize size = rect.size;
CGFloat width = size.width;
CGFloat height = size.height;
CGFloat scale_screen = [UIScreen mainScreen].scale;
if (self.view and !changeViewFrameV)
{
//根据分辨率判断手机型号
if (width*scale_screen == 2436 and height*scale_screen == 1125)//iPhoneX,iphoneXS
{
//在标准的safeAreaInsets区域的基础上再拓宽一定范围 shanwl
// UIEdgeInsets a = self.view.layoutMargins;
// UIEdgeInsets b = self.view.safeAreaInsets;
self.view.frame = CGRectMake(self.view.safeAreaInsets.left - 13,
0,
self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right + 26,
self.view.frame.size.height);
}
else if ((width*scale_screen == 1792 and height*scale_screen == 828) or //iphone XR
(width*scale_screen == 2688 and height*scale_screen == 1242)) //iphone XS MAX
{
//标准的safeAreaInsets区域
self.view.frame = CGRectMake(self.view.safeAreaInsets.left,
0,
self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right,
self.view.frame.size.height);
}
changeViewFrameV = true;
}
}
}
3、如果有横竖屏切换的需求
在AppController.mm 函数didFinishLaunchingWithOptions中加入
//横屏初始化
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
viewController.view = eaglView;
//竖屏初始化
viewControllerV = [[RootViewControllerV alloc] initWithNibName:nil bundle:nil];
viewControllerV.wantsFullScreenLayout = YES;
// Set RootViewController to window
if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
{
// warning: addSubView doesn't work on iOS6
[window addSubview: viewController.view];
}
else
{
// use this method on ios6
[window setRootViewController:viewController];
}
新增切换函数changedActivityOrientation
+(void)changedActivityOrientation:(NSDictionary*) dict
{
NSObject* obj = [dict objectForKey:@"type"];
if (nil != obj)
{
int type = [obj intValue];
AppController * pApp = (AppController*)[[UIApplication sharedApplication] delegate];
CCEAGLView* glView = pApp->eaglView;
if (type == 1)//横屏
{
pApp->viewControllerV.view = nil;
pApp->viewController.view = glView;
if ([[UIDevice currentDevice].systemVersion floatValue] < 6.0)
{
[pApp->window addSubview:pApp->viewController.view];
}
else
{
[pApp->window setRootViewController:pApp->viewController];
}
}
else //竖屏
{
pApp->viewController.view = nil;
pApp->viewControllerV.view = glView;
if ([[UIDevice currentDevice].systemVersion floatValue] < 6.0)
{
[pApp->window addSubview:pApp->viewControllerV.view];
}
else
{
[pApp->window setRootViewController:pApp->viewControllerV];
}
}
[pApp->window makeKeyAndVisible];
}
}