1、引入Launch Screen Storyboard布局文件
2、
这样是解决了全屏的问题,但是因为齐刘海的问题,有些按钮都被遮挡住了,如果单独对这些遮挡的按钮进行位置操作的话,整个ui布局效果就会很差,而且针对每一个被遮挡的控件界面都要操作,工作量还是比较大的,我们游戏也已经开发了一些时日,所以在这个基础上进行了一些修改,采用两边留出一部分用图片填充,游戏UI都在中间部分展示的方式
首先,在 RootViewController.mm文件里添加方法“viewSafeAreaInsetsDidChange”,这个方法是干嘛的在xcode Quick help里都有介绍,这里就不说了
- (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;
}
}
}
想要把iPhoneX下的横条隐藏掉,点击后出现,下面就是它的方法
- (BOOL)prefersHomeIndicatorAutoHidden{
return YES;
}
3、其次,在AppController.mm 函数didFinishLaunchingWithOptions中加入
// 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];
}
[window makeKeyAndVisible];
[[UIApplication sharedApplication]setStatusBarHidden:true];
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;
//通过分辨率判断是否是iPhoneX手机
if (width*scale_screen == 2436 and height*scale_screen == 1125)
{
UIView *viewLeft = [[UIView alloc]initWithFrame:CGRectMake(0,0,31,viewController.view.frame.size.height)];
UIImageView* imageViewL = [[UIImageView alloc]initWithFrame:viewLeft.bounds];
imageViewL.image = [UIImage imageNamed:@"bg_back.png"];
[viewLeft addSubview:imageViewL];
[window addSubview:viewLeft];
[window sendSubviewToBack:viewLeft];
UIView *viewRight = [[UIView alloc]initWithFrame:CGRectMake(viewController.view.frame.size.width + 31,0,31,viewController.view.frame.size.height)];
UIImageView* imageView = [[UIImageView alloc]initWithFrame:viewRight.bounds];
imageView.image = [UIImage imageNamed:@"bg_back.png"];
[viewRight addSubview:imageView];
[window addSubview:viewRight];
[window sendSubviewToBack:viewRight];
}
}
// IMPORTANT: Setting the GLView should be done after creating the RootViewController
cocos2d::GLView *glview =cocos2d::GLViewImpl::createWithEAGLView((__bridgevoid *)viewController.view);
cocos2d::Director::getInstance()->setOpenGLView(glview);
//run the cocos2d-x game scene
app->run();
return YES;
这样操作之后UI都显示在中间区域部分了,但是后来在运行测试的时候,又发现了一个bug,就是在输入框输入东西的时候,整个视图做移动缩放动画,位置更改了,通过debug 之后发现在文件“CCEAGLView-ios.mm” 下图这个方法里添加额外处理:
- (id) initWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth preserveBackbuffer:(BOOL)retained sharegroup:(EAGLSharegroup*)sharegroup multiSampling:(BOOL)sampling numberOfSamples:(unsigned int)nSamples
{
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;
//通过分辨率判断是否是iPhoneX手机
if (width*scale_screen == 2436 and height*scale_screen == 1125)
{
frame = CGRectMake(31,0,750,375);
}
}
//其他的操作
}
4、如果有横竖屏切换的需求
//横屏初始化
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
viewController.view = eaglView;
//竖屏初始化
viewControllerV = [[RootViewControllerV alloc] initWithNibName:nil bundle:nil];
viewControllerV.wantsFullScreenLayout = YES;
新增切换函数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];
}
}