导航栏的坑 (导航透明/导航除线/titleIView)

//以下四个条件缺一不可

/1.必须是半透明状态
self.navigationBar.translucent = YES;
//2.导航栏背景图片为空图片 (不可以设置backgroundColor或者barTintColor, 没有效果的)
[self.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics: UIBarMetricsDefault];
//3.阴影图片为空图片 (不可以设置为nil,没用的)
[self.navigationBar setShadowImage:[[UIImage alloc] init]];

//其实到导航栏才44px, 但是backgroundView有64px 
self.navigationBar.layer.masksToBounds = YES;

导航除线

self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    UIImage *img = [UIImage imageWithColor:[UIColor greenColor] size:CGSizeMake(100, 100)];
//必须设置一张图片而不是设置背景颜色
    [self.navigationController.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];

3.设置设置titleView时, titleView的宽度和高度无法自由设置

设置搜索案例

如果已经设置了默认返回的图标, 你会发现设置titleview时, 你的titleview左边会空出一块

UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 500, 30)];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:tf];
    self.navigationItem.leftBarButtonItem = item;
原文地址:https://www.cnblogs.com/apem/p/5254389.html