1. 程式人生 > >導航欄中navigationItem自定義的兩種寫法

導航欄中navigationItem自定義的兩種寫法

 HMDiaryNavButton *filterButton = [[HMDiaryNavButton alloc]init];
    [filterButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [filterButton setFrame:CGRectMake(0, 0, 35, 40)];
    [filterButton setTitle:@"篩選" forState:UIControlStateNormal];
    [filterButton setImage:[UIImage imageNamed:@"diary_filter.png"] forState:UIControlStateNormal];
    [filterButton addTarget:self action:@selector(filterButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *fItem = [[UIBarButtonItem alloc] initWithCustomView:filterButton];
    
    
    HMDiaryNavButton *editButton = [[HMDiaryNavButton alloc]init];
    [editButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [editButton setFrame:CGRectMake(0, 0, 35, 40)];
    [editButton setTitle:@"寫日記" forState:UIControlStateNormal];
    [editButton setImage:[UIImage imageNamed:@"diary_edit.png"] forState:UIControlStateNormal];
    [editButton addTarget:self action:@selector(editButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *eItem = [[UIBarButtonItem alloc] initWithCustomView:editButton];
    
    
    self.navigationItem.rightBarButtonItems = @[eItem, fItem];



 UIView *rightNavView = [[UIView alloc]init];
    rightNavView.backgroundColor = [UIColor clearColor];
    rightNavView.frame = CGRectMake(0, 0, 45*3, 45);
    rightNavView.userInteractionEnabled = YES;
    
    HMDiaryButton *praiseButton = [[HMDiaryButton alloc]init];
    praiseButton.frame = CGRectMake(0, 0, 45, 45);
    [praiseButton setImage:[UIImage imageNamed:@"diary_praise.png"] forState:UIControlStateNormal];
    [praiseButton setImage:[UIImage imageNamed:@"diary_praiseH.png"] forState:UIControlStateHighlighted];
    [praiseButton setImage:[UIImage imageNamed:@"diary_praiseH.png"] forState:UIControlStateSelected];
    [praiseButton addTarget:self action:@selector(praiseButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [rightNavView addSubview:praiseButton];
    _praiseButton = praiseButton;
    
    HMDiaryButton *commentButton = [[HMDiaryButton alloc]init];
    commentButton.frame = CGRectMake(45, 0, 45, 45);
    [commentButton setImage:[UIImage imageNamed:@"diary_comment.png"] forState:UIControlStateNormal];
    //[commentButton setImage:[UIImage imageNamed:@"diary_commentH.png"] forState:UIControlStateHighlighted];
    [commentButton setImage:[UIImage imageNamed:@"diary_commentH.png"] forState:UIControlStateSelected];
    [commentButton addTarget:self action:@selector(commentButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [rightNavView addSubview:commentButton];
    _commentButton = commentButton;
    
    HMDiaryButton *collectButton = [[HMDiaryButton alloc]init];
    collectButton.frame = CGRectMake(45*2, 0, 45, 45);
    [collectButton setImage:[UIImage imageNamed:@"diary_collect.png"] forState:UIControlStateNormal];
    //[collectButton setImage:[UIImage imageNamed:@"diary_collectH.png"] forState:UIControlStateHighlighted];
    [collectButton setImage:[UIImage imageNamed:@"diary_collectH.png"] forState:UIControlStateSelected];
    [collectButton addTarget:self action:@selector(collectButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [rightNavView addSubview:collectButton];
    _collectButton = collectButton;
    
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc]initWithCustomView:rightNavView];
    self.navigationItem.rightBarButtonItem = barItem;