iOS 11ios实用开发技巧iOS Developer

iPhone X适配(为将导航栏高度写死的提供思路)

2017-11-02  本文已影响111人  Ozhy1991yhzO

iPhone X的简单适配。以前的导航栏高度直接 64,tabbar 49写死,不要问我为什么这么作死。

一,启动图适配

iPhone X启动图大小:1125 × 2436 pixels。
没X的启动图是这样

无启动图

有启动图

有启动图

二、导航栏的适配

就直接写个简单的了,看了思路应该都知道怎么做。
(1)当你使用的是系统的导航栏,tabbar时,没毛病;系统会自动为你适配

用系统的Nav,TabBar

(2)当导航栏是自定义的,你还还规定高度为64(作死),以下是适配前的代码和效果图

-(void)creatNavView
{
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 64)];
    navView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:navView];
    
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20+44/2-10, KScreenWidth, 20)];
    [navView addSubview:titleLab];
    titleLab.font = [UIFont systemFontOfSize:16];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.text = @"首页";
}

在iPhone X会少一大节,看图。

高度是64的Nav

怎么适配,肯定要改为全局的,一般只在继承的父视图里面改下自定义的导航栏的高度,在逐个更改导航栏上面的子视图的frame。

适配代码,与适配前的对比着看下。主要改变在frame。

define K_StatusBar_Height ([[UIScreen mainScreen] currentMode].size.height == 2436.00 ? 44 : 20)
//得到statueBar的高度,在iPhone X是44,其他的上面是20
#define K_StatusBar_Height  ([[UIScreen mainScreen] currentMode].size.height == 2436.00 ? 44 : 20)
//代码改为
-(void)creatNavView
{
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, K_StatusBar_Height+44)];
    navView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:navView];
    
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, K_StatusBar_Height+44/2-10, KScreenWidth, 20)];
    [navView addSubview:titleLab];
    titleLab.font = [UIFont systemFontOfSize:16];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.text = @"首页";
}
导航栏适配后

二、底部的适配

适配思路和导航栏一样。

适配前,底部高度规定死,会遮盖iPhone X的底部交互的那条线

-(void)layOutBottomView
{
    UIView *bottomView =[[UIView alloc]initWithFrame:CGRectMake(0, KScreenHeight-(49), KScreenWidth, (49))];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    
    UIImageView *lineIma =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, K_Line_width)];
    [bottomView addSubview:lineIma];
    lineIma.backgroundColor = [UIColor redColor];

    NSArray  *imageStrArr =[NSArray arrayWithObjects:@"ic_product_details_browse_the_document",@"ic_product_details_get_information", @"ic_product_details_service",nil];
    
    float btnWidth = 20;
    float pandding = (KScreenWidth-20*3)/4;
    float btnHeight = CGRectGetHeight(bottomView.frame);
    for (int i=0; i<imageStrArr.count; i++)
    {
        UIImageView *ima = [UIImageView new];
        [bottomView addSubview:ima];
        ima.frame = CGRectMake(pandding+(btnWidth+pandding)*i, K_Line_width, btnWidth, btnHeight-K_Line_width);
        ima.image = [UIImage imageNamed:imageStrArr[i]];
        ima.contentMode = UIViewContentModeScaleAspectFit;
    }
    
}
底部高度规定

稍加改动(底部适配)

适配代码,与适配前的对比着看下

define K_BottomBar_Height ([[UIScreen mainScreen] currentMode].size.height == 2436.00 ? 34 : 0)
//底部高度宏,iPhone X上为34,其他为0 
#define K_BottomBar_Height  ([[UIScreen mainScreen] currentMode].size.height == 2436.00 ? 34 : 0)
-(void)layOutBottomView
{
    UIView *bottomView =[[UIView alloc]initWithFrame:CGRectMake(0, KScreenHeight-(K_BottomBar_Height+49), KScreenWidth, (K_BottomBar_Height+49))];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    
    UIImageView *lineIma =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, K_Line_width)];
    [bottomView addSubview:lineIma];
    lineIma.backgroundColor = [UIColor redColor];

    NSArray  *imageStrArr =[NSArray arrayWithObjects:@"ic_product_details_browse_the_document",@"ic_product_details_get_information", @"ic_product_details_service",nil];
    
    float btnWidth = 20;
    float pandding = (KScreenWidth-20*3)/4;
    float btnHeight = CGRectGetHeight(bottomView.frame)-K_BottomBar_Height;
    for (int i=0; i<imageStrArr.count; i++)
    {
        UIImageView *ima = [UIImageView new];
        [bottomView addSubview:ima];
        ima.frame = CGRectMake(pandding+(btnWidth+pandding)*i, K_Line_width, btnWidth, btnHeight-K_Line_width);
        ima.image = [UIImage imageNamed:imageStrArr[i]];
        ima.contentMode = UIViewContentModeScaleAspectFit;
    }
    
}

效果图:

底部适配
代码参考链接:https://pan.baidu.com/s/1o7CrHB8
上一篇 下一篇

猜你喜欢

热点阅读