自定义NavigationItem 的 左 、右 按键

#import "AZDetailViewController.h"
#import "AZMainViewController.h"
#import "AZValueSign.h"

#define Y 64
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface AZDetailViewController ()
@property (nonatomic,strong)NSMutableArray *btnArray;
@end

@implementation AZDetailViewController
-(NSMutableArray *)btnArray
{
    if (!_btnArray) {
        _btnArray=[NSMutableArray array];
    }
    return _btnArray;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
#warning -- 带修改navigationItem的背景为纯色
- (void)viewDidLoad
{
    [super viewDidLoad];
    AZValueSign *vaule=[AZValueSign defaultValue];
    NSString *name=vaule.dic[@"detailName"];
    self.navigationController.navigationBarHidden=NO;
    self.navigationItem.title=name;
#if 0
    //带修改navigationItem的背景为纯色
    UIImage *bgImage=[UIImage imageNamed:@"bg_white.png"];
    UIImageView *gbImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];
    gbImageView.image=bgImage;
    [self.navigationItem.titleView addSubview:gbImageView];
#endif
    [self createBackBtn];
    [self createShareBtn];
    [self createCollectBtn];
    
    self.navigationItem.rightBarButtonItems=self.btnArray;
    //height = 64
//    NSLog(@"h=%f",self.navigationController.navigationBar.frame.origin.y+self.navigationController.navigationBar.frame.size.height);
    

    
    
    
    
}
#pragma mark -- 自定义navigationBar按键
//创建返回按键
-(void)createBackBtn
{
    UIImage *leftImage=[UIImage imageNamed:@"ic_xq_back.png"];
    [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *leftHightImage=[UIImage imageNamed:@"ic_xq_back_pressed.png"];
    [leftHightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIView *homeButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 40)];
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:leftImage forState:UIControlStateNormal];
    [btn setImage:leftHightImage forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    btn.frame =CGRectMake(2, 8, 25, 25);
    
    [homeButtonView addSubview:btn];
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc]initWithCustomView:homeButtonView];
    
    self.navigationItem.leftBarButtonItem=leftButtonItem;
}

//创建收藏按键
-(void)createCollectBtn
{
    UIImage *leftImage=[UIImage imageNamed:@"ic_xq_collect.png"];
    [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *leftHightImage=[UIImage imageNamed:@"ic_xq_collect_pressed.png"];
    [leftHightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIView *collectButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 260, 30, 40)];
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:leftImage forState:UIControlStateNormal];
    [btn setImage:leftHightImage forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(collectClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.frame =CGRectMake(2, 8, 25, 25);
    
    [collectButtonView addSubview:btn];
    UIBarButtonItem *collectBtnItem = [[UIBarButtonItem alloc]initWithCustomView:collectButtonView];
    [self.btnArray addObject:collectBtnItem];
   
}

//创建分享按键
-(void)createShareBtn
{
    UIImage *leftImage=[UIImage imageNamed:@"ic_xq_share.png"];
    [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *leftHightImage=[UIImage imageNamed:@"ic_xq_share_pressed.png"];
    [leftHightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIView *homeButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 290, 30, 40)];
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:leftImage forState:UIControlStateNormal];
    [btn setImage:leftHightImage forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(shareBtnClick) forControlEvents:UIControlEventTouchUpInside];
    btn.frame =CGRectMake(2, 8, 25, 25);
    
    [homeButtonView addSubview:btn];
    UIBarButtonItem *shareButtonItem = [[UIBarButtonItem alloc]initWithCustomView:homeButtonView];
    [self.btnArray addObject:shareButtonItem];
   
}

#pragma mark -- 返回
-(void)backClick
{
    self.navigationController.navigationBarHidden=YES;
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -- 收藏
-(void)collectClick:(UIButton *)btn
{
    UIImage *leftImage=[UIImage imageNamed:@"ic_xq_collect_ok.png"];
    [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [btn setImage:leftImage forState:UIControlStateNormal];
    
    NSLog(@"收藏");
}
#pragma mark -- 分享
-(void)shareBtnClick
{
    NSLog(@"分享");
}



图:自定义NavigationItem 的 左 、右 按键_第1张图片

你可能感兴趣的:(自定义NavigationItem 的 左 、右 按键)