有时候,app要统一风格,对于没有特别要求的地方,采用自定义默认的样式。下面是本人自己的样式,有其他需求的自己试着去改一改。
1、先看一下特殊样式
title为图片,左右item都自定义
因为这个需求只是在某些界面才会用,所以,下面的代码是放在ViewController中的
//title
UIView* titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 14, 80, 16)];
UIImageView *titleImg_view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"personal_headerImg0"]];//HomePageImg_bar_title
[titleView addSubview:titleImg_view];
self.navigationItem.titleView = titleView;
//left
UIButton *leftItem = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,32, 32)];
UIImage *imageleft = [UIImage imageNamed:@"HomePageImg_bar_left"];//
[leftItem setBackgroundImage:imageleft forState:UIControlStateNormal];
[leftItem addTarget:self action:@selector(leftClickAction) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftItem];
//right
UIButton *rightItem = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,18, 18)];
UIImage *imageRight = [UIImage imageNamed:@"HomePageImg_bar_right"];//
[rightItem setBackgroundImage:imageRight forState:UIControlStateNormal];
[rightItem addTarget:self action:@selector(rightClickAction) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightItem];
2、统一样式
一般的二级界面样式
这个样式是整个app中的基本样式,为了不在每一个ViewController中进行设置,所以建了一个BaseNavigationControlle继承 UINavigationController
BaseNavigationControlle.m
#import "BaseNavigationController.h"
@interface BaseNavigationController ()
@end
@implementation BaseNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view. @"bracket_left_black"
//整个导航栏的颜色
[self.navigationBar setBarTintColor:[UIColor whiteColor]];
//所有按钮文字的颜色
[self.navigationBar setTintColor:[UIColor colorWithHexString:@"333333"]];
//title文字颜色
[self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"333333"],NSFontAttributeName:[UIFont systemFontOfSize:16]}];
}
#pragma mark - 重载父类进行改写
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//先进入子Controller
[super pushViewController:viewController animated:animated];
//替换掉leftBarButtonItem
if (viewController.navigationItem.leftBarButtonItem== nil && [self.viewControllers count] > 1) {
viewController.navigationItem.leftBarButtonItems =[self customLeftBackButton];
}
}
#pragma mark - 自定义返回按钮图片
-(NSArray*)customLeftBackButton{
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 40, 16);
// UIImage *image = [UIImage imageCompressForWidth:[UIImage imageNamed:@"bracket_left_black"] targetWidth:18];
UIImage *image = [UIImage imageNamed:@"bracket_left_black"];
[backButton setImage:image forState:UIControlStateNormal];
backButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
[backButton addTarget:self
action:@selector(popAction)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSeperator.width = -20;
// 注意下面这个顺序不要写错了,写错了顺序会导致写不出我们想要的效果
return [NSArray arrayWithObjects:negativeSeperator,backItem,nil];
}
#pragma mark - 返回按钮事件
-(void)popAction
{
[self popViewControllerAnimated:YES];
}
@end
网友评论