iOS OC 导航栏自定义rightBarButtonItem

复制代码
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    customView.backgroundColor = [UIColor lightGrayColor];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 34, 80, 10)];
    label.text = @"Custom";
    label.textAlignment = NSTextAlignmentCenter;
    [customView addSubview:label];
    
    UIImage *image = [UIImage imageNamed:@"collect"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(33, 0, 34, 34)];
    [button setImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(collectBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [customView addSubview:button];

    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
    self.navigationItem.rightBarButtonItem = barButtonItem;//rightBarButtonItems是一样的(多个的时候用s)
复制代码

 

下面我自己用的

 UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    containerView.backgroundColor = [UIColor clearColor];
    [containerView addSubview:self.customerServiceButton];
    // 将自定义按钮添加到UIBarButtonItem中
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:containerView];
    // 将按钮添加到导航项的右侧
    self.navigationItem.rightBarButtonItems = @[rightBarButton];
@property (nonatomic, strong) QMUIButton *customerServiceButton;//客服按钮
复制代码
#pragma mark - Setter && Getter 懒加载
//客服按钮
-(QMUIButton *)customerServiceButton{
    if (!_customerServiceButton) {
        _customerServiceButton = [QMUIButton buttonWithType:UIButtonTypeCustom];
        [_customerServiceButton setFrame:CGRectMake(76, 10, 24, 24)];
        [_customerServiceButton setImage:UIImageMake(@"下载 2") forState:UIControlStateNormal];
        [_customerServiceButton addTarget:self action:@selector(customerServiceButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _customerServiceButton;
}

- (void)customerServiceButtonEvent{
    NSLog(@"用户点击了客服按钮888");
    ZXJKeFuViewController *vc= [[ZXJKeFuViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
复制代码

 有人这样写

复制代码
- (void)createRightItem{
    
 
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem qmui_itemWithImage:UIImageMake(@"下载 2") target:self action:@selector(rightClick)];
    
    self.navigationItem.rightBarButtonItem.tintColor = UIColorMakeWithHex(@"#292929");
}

- (void)rightClick{
    
    [self addNewAdsClick];

    
}

- (void)addNewAdsClick
{
    ZXJKeFuViewController* vc= [[ZXJKeFuViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
}
复制代码

也是可以的

posted on   高彰  阅读(33)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
历史上的今天:
2021-11-19 iOS 切后台挂机
2021-11-19 iOS 导航栏translucent用法
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示