iOS之Nav状态栏与TabBar条相关处理

本文介绍如何在iOS应用中定制状态栏、导航栏及标签栏的样式,包括改变颜色、字体及实现透明效果等,并提供了解决CATextLayer字体模糊的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

##一 如何改变状态栏的字体电池条颜色 1 需要在 info.plist 中添加一个字段,否则使用下面的代码会无效.

2 在 AppDelegate的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; 执行一个方法.

// 状态栏字体电池条等设置成白色.
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
复制代码

##二 如何删除导航栏下的黑线

// 使用递归遍历所有的层级的 view 层,我们可以看到有一个 height=0.5的 imageView, 把这个删掉就好了.
- (void)getSubViews:(UIView *)view { 
  for (UIView *subView in view.subviews) { 
    if (subView.subviews.count) {
       [self getSubViews:subView]; 
    } else { 
       if (subView.frame.size.height <= 1) { 
          [subView removeFromSuperview]; 
          } 
      } 
   }
}
复制代码

##三 如何设置导航栏标题的字体 有两种方案: 方案一: 使用带属性的字符串,这种方法简单明了.

UIColor *color = [UIColor redColor];
NSDictionary *dict = @{ NSForegroundColorAttributeName : color, NSFontAttributeName : [UIFont systemFontOfSize:20] }; self.navigationController.navigationBar.titleTextAttributes = dict;
复制代码

方案二: 设置navigationItem.titleView. 给titleView赋值一个 label, 这样子的话修改起来都是常规做法.

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 44)];
label.text = @"title标题"; 
label.textColor = [UIColor redColor]; 
label.textAlignment = NSTextAlignmentCenter; self.navigationItem.titleView = label;
复制代码

##四 使 navi 变成透明的 有两种方案: 方案一:

for (UIView *view in self.navigationController.navigationBar.subviews) { 
if ([view isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]) {
 [view removeFromSuperview];
 } 
}
复制代码

方案二: 放一张空白图片

// 设置导航栏透明
- (void)viewDidLoad {
    [super viewDidLoad]; 
   
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"tm"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    self.navigationController.navigationBar.translucent = YES;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil]; 
}

复制代码

##五 如何避免 CATextLayer 中的字体模糊 由于现在是Retina 屏幕,使用 CATextLayer时,设置完字体后显示会模糊.下面看一下映射关系.非Retina:1 Point = 1 x 1 PixelRetina:1 Point = 2 x 2 Pixel由于 Retina 屏一个 Point 映射4个(2 * 2) Pixel,所以 scale为2.所以这时牵扯到一个属性:contentsScale

CATextLayer *layer = [[CATextLayer alloc] init];
layer.frame = CGRectMake(100, 100, 200, 200); layer.backgroundColor = [UIColor redColor].CGColor;
layer.string = @"hahah"; 
layer.fontSize = 20; 
layer.contentsScale = 2; // 这个是重点,不设置的话, string 就会模糊 [self.view.layer addSublayer:layer];
复制代码

##六 如何修改项目的名称 由于创建项目时,项目名称包含中文会乱码显示为-,Bundle Identifier 中会直接使用-代替中文.所以项目名称要使用英文,实在不行要使用拼音.但安装到手机中的名称要显示自己想要的名称的话.需要在** info.plist ** 中添加一项.** Bundle display name **.

对应的 Value 就是安装app 后,展示的名称.

##七 移除taBar上黑线

- (void)removeTabBarBlackLine:(UIView *)view {
    for (UIView *subView in view.subviews) {
        if (subView.subviews.count) {
            [self removeTabBarBlackLine:subView];
        } else {
            
            if (subView.frame.size.height <= 1) {
                
                subView.hidden = YES;
            }
        }
    }
}
复制代码

##八 当需要背景图时这个时候自定义 tabBar

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        // tabbar相关背景设置
        UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar"]];
   bg.frame = CGRectMake(0, 0, self.width, self.height);
        bg.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:bg];
       
    }
    return self;
}
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值