iOS全局定义NavigationController样式

1、在自定义的NavigationController.m的initialize方法中,写入下面的代码,

+ (void)initialize{
    // 获取当前类下面的UIBarButtonItem
    UIBarButtonItem *item = [UIBarButtonItem appearanceWhenContainedIn:self, nil];

    // 设置导航条按钮的文字颜色
    NSMutableDictionary *titleAttr = [NSMutableDictionary dictionary];
    titleAttr[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [item setTitleTextAttributes:titleAttr forState:UIControlStateNormal];

    // 1.设置导航栏背景
    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    [bar setBackgroundImage:[UIImage imageWithOriginalName:@"bg_navigation bar"] forBarMetrics:UIBarMetricsDefault];

    // 状态栏显示样式
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

    // 2.设置导航栏文字属性
    NSMutableDictionary *barAttrs = [NSMutableDictionary dictionary];
    [barAttrs setObject:[UIColor orangeColor] forKey:NSForegroundColorAttributeName];
    [barAttrs setObject:[NSValue valueWithUIOffset:UIOffsetMake(0, 0)] forKey:UITextAttributeTextShadowOffset];
    [bar setTitleTextAttributes:barAttrs];

// 4.back按钮
// [item setBackButtonBackgroundImage:[UIImage imageWithOriginalName:@"navigation_back"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    UIImage *image = [UIImage imageNamed:@"navigation_back"];
    [item setBackButtonBackgroundImage:[image resizableImageWithCapInsets:UIEdgeInsetsMake(0, image.size.width, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [item setBackButtonTitlePositionAdjustment:UIOffsetMake(-200.f, 0) forBarMetrics:UIBarMetricsDefault];
}

这样的话,就能够全局定义样式了,在自定义navigationItem.backBarButtonItem的时候需要注意一下,下一篇会介绍backBarButtonItem的用法。

你可能感兴趣的:(ios,样式,Navigation)