05-导航条

一、自定义的导航条

  • 设置所有导航条的背景图片文字属性等
+(void)initialize
{
    /** 设置UINavigationBar */
    UINavigationBar *nav = [UINavigationBar appearance];
    
    // 导航栏背景色图片
    [nav setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
    
    // 设置文字标题属性
    NSMutableDictionary *titleAttr = [NSMutableDictionary dictionary];
    titleAttr[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    
    [nav setTitleTextAttributes:titleAttr];
}

补充:为什么用+(void)initialize方法

  • initialize 方法类似一个懒加载,如果没有使用这个类,那么系统默 认不会去调用这个方法,且默认只加载一次;+ initialize 很有趣,因为它是懒调用的,也有可能完全不被调用。
  • initialize 的调用发生在 +init 方法之前。
  • load 方法会在加载类的时候就被调用,也就是 ios 应用启动的时候,就会加载所有的类,就会调用每个类的 + load 方法。

  • 重写push方法-(主要是跳转控制器时的返回)
/**
   *  重写push方法的目的 : 拦截所有push进来的子控制器
   *
   *  @param viewController 刚刚push进来的子控制器
   */
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // // 如果viewController不是最早push进来的子控制器
    if (self.childViewControllers.count > 0) {
        
        // 左上角“返回“
        UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [back setImage: [UIImage imageNamed:@"navigationButtonReturn"]  forState:UIControlStateNormal];
        [back setImage: [UIImage imageNamed:@"navigationButtonReturnClick"]  forState:UIControlStateSelected];
        [back setTitle:@"返回" forState:UIControlStateNormal];
        [back setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [back setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
        [back sizeToFit];

      // 监听返回
        [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];  
       
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:back];
        
        // 隐藏底部tabbar
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:YES];
}
-(void)back
{
    [self popViewControllerAnimated:YES];
}

二、导航栏的标题在各自的控制器里设置

第一种:标题为图片
// 标题
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];

imageView的创建方式有两种:
[imageView alloc] init] : 创建完没有尺寸
[imageView alloc] initWithImage:~~~] : 只要图片传进去,图片大小 = imageVIew的大小

第二种:标题为文字
// 标题
    self.navigationItem.title = @"我的关注";

三、导航栏左右角的UIBarButtonItem

05-导航条_第1张图片
2CF6B68B-120D-457B-83F8-8233226A3186.png

所有控制器的导航条的UIBarButtonItem,可抽取出来,做个UIBarButtonItemCategory

+(instancetype)itemWithImage:(NSString *)image highImage:(NSString *)highImage target:(id)target  action:(SEL)action
{

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    [btn addTarget:target action:action forControlEvents:UIControlEventAllTouchEvents];
    
    [btn sizeToFit];
    
    return [[self alloc] initWithCustomView:btn];
}

各控制器设置左右角item

第一种:
// 左上角
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"MainTagSubIcon" highImage:@"MainTagSubIconClick" target:self action:@selector(tagClick)];
第二种:
    // 右边——设置
    UIBarButtonItem *settingItem = [UIBarButtonItem itemWithImage:@"mine-setting-icon" highImage:@"mine-setting-icon-click" target:self action:@selector(settingClick)];
    // 右边——月亮
    UIBarButtonItem *moonItem = [UIBarButtonItem itemWithImage:@"mine-moon-icon" highImage:@"mine-moon-icon-click" target:self action:@selector(settingClick)];
    
    // 右边
    self.navigationItem.rightBarButtonItems = @[settingItem, moonItem];

你可能感兴趣的:(05-导航条)