美文网首页iOS劝退指南
NavigationController统一设置自定义导航条

NavigationController统一设置自定义导航条

作者: MccReeee | 来源:发表于2016-11-08 12:12 被阅读482次

创建个BaseNavigationController继承自UINavigationController
重写push方法

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
//设置导航条背景图片
    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_bg"] forBarMetrics:UIBarMetricsDefault];
//设置标题颜色
    self.navigationBar.tintColor = [UIColor whiteColor];
//通过Attributes设置标题字体颜色字号
    [self.navigationBar setTitleTextAttributes:@{@"NSForegroundColorAttributeName":[UIColor whiteColor]}];
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    textAttrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:18];
    [self.navigationBar setTitleTextAttributes:textAttrs];

//如果是根控制器就不显示返回按钮
    if (self.childViewControllers.count > 0) { // 非根控制器
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"button_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnPressed)];
        viewController.navigationItem.leftBarButtonItem = item;
    }
    [super pushViewController:viewController animated:animated];
}

相关文章

网友评论

    本文标题:NavigationController统一设置自定义导航条

    本文链接:https://www.haomeiwen.com/subject/gmuauttx.html