1.自定义TitleButton
#import <UIKit/UIKit.h>
@interface MRTitleButton : UIButton
+ (instancetype)titleButton;
@end
#import "MRTitleButton.h"
#import "UIImage+MJ.h"
#define IWTitleButtonImageW 20
@implementation MRTitleButton
+ (instancetype)titleButton
{
return [[self alloc] init];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 高亮的时候不要自动调整图标
self.adjustsImageWhenHighlighted = NO;
self.titleLabel.font = [UIFont boldSystemFontOfSize:19];
self.imageView.contentMode = UIViewContentModeCenter;
self.titleLabel.textAlignment = NSTextAlignmentRight;
// 背景
[self setBackgroundImage:[UIImage resizedImageWithName:@"navigationbar_filter_background_highlighted"] forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
return self;
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloat imageY = 0;
CGFloat imageW = IWTitleButtonImageW;
CGFloat imageX = contentRect.size.width - imageW;
CGFloat imageH = contentRect.size.height;
return CGRectMake(imageX, imageY, imageW, imageH);
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGFloat titleY = 0;
CGFloat titleX = 0;
CGFloat titleW = contentRect.size.width - IWTitleButtonImageW;
CGFloat titleH = contentRect.size.height;
return CGRectMake(titleX, titleY, titleW, titleH);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
2.MRHomeViewController添加自定义按钮
#import "MRHomeViewController.h"
#import "MRNavigationController.h"
#import "Header.h"
#import "UIImage+MJ.h"
#import "UIBarButtonItem+MJ.h"
#import "MRTitleButton.h"
@interface MRHomeViewController ()
@end
@implementation MRHomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
//左边按钮
self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"navigationbar_friendsearch" highIcon:@"navigationbar_friendsearch_highlighted" action:@selector(findFriend) target:self];
//右边按钮
self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithIcon:@"navigationbar_pop" highIcon:@"navigationbar_pop_highlighted" action:@selector(pop) target:self];
//中间按钮
MRTitleButton *titleButton = [[MRTitleButton alloc]init];
titleButton.bounds = CGRectMake(0, 0, 100, 40);
[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
[titleButton setTitle:@"好友圈" forState:UIControlStateNormal];
[titleButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = titleButton;
}
- (void)titleClick:(MRTitleButton *)titleButton
{
if (titleButton.tag == -1) {
[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
titleButton.tag = 0;
} else {
[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal];
titleButton.tag = -1;
}
}
-(void)findFriend
{
NSLog(@"--------findFriend");
}
-(void)pop
{
NSLog(@"------------pop");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
// Configure the cell...
cell.textLabel.text = @"HHHHHHHH";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *vc = [[UIViewController alloc]init];
vc.view.backgroundColor = [UIColor redColor];
//隐藏下面的TabBar
//vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];
}
@end
3.UIBarButtonItem添加分类
+ (UIBarButtonItem*)itemWithIcon:(NSString*)icon highIcon:(NSString*)highIcon action:(SEL)action target:(id)target
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageWithName:highIcon] forState:UIControlStateHighlighted];
button.bounds = CGRectMake(0, 0, button.currentBackgroundImage.size.width , button.currentBackgroundImage.size.height);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return [[UIBarButtonItem alloc]initWithCustomView:button];
}
结果图: