自定义导航栏上的按钮

笑着哭i 提交于 2020-12-23 04:23:46

1、在导航栏左边自定义一个按钮

UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 15, 40, 40)];
    
    leftButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [leftButton setTitleColor:UIColorFromRGB(0x19191a) forState:UIControlStateNormal];
    [leftButton setTitle:@"账单" forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(didList:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarButtonItems = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    
    UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                   target:nil action:nil];
//    nagetiveSpacer.width = -15;//这个值可以根据自己需要自己调整
    self.navigationItem.leftBarButtonItems = @[nagetiveSpacer, leftBarButtonItems];

2、导航栏右边自定义两个按钮

UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 15, 20, 17)];
    
    rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightButton setTitleColor:UIColorFromRGB(0x19191a) forState:UIControlStateNormal];
    [rightButton setImage:IMAGENAMED(@"storehouse") forState:UIControlStateNormal];
//    [leftButton setTitle:@"账单" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(didList:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarButtonItems = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
//    UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
//                                                                                   target:nil action:nil];
//    nagetiveSpacer.width = 40;//这个值可以根据自己需要自己调整
//    self.navigationItem.rightBarButtonItems = @[nagetiveSpacer, rightBarButtonItems];
    
    UIButton *rightButtonMore = [[UIButton alloc] initWithFrame:CGRectMake(220, 15, 20, 17)];
    
    rightButtonMore.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightButtonMore setTitleColor:UIColorFromRGB(0x19191a) forState:UIControlStateNormal];
    [rightButtonMore setImage:IMAGENAMED(@"Treasure_more") forState:UIControlStateNormal];
    //    [leftButton setTitle:@"账单" forState:UIControlStateNormal];
    [rightButtonMore addTarget:self action:@selector(clickMore:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarButtonItemsMore = [[UIBarButtonItem alloc] initWithCustomView:rightButtonMore];
    
    UIBarButtonItem *nagetiveSpacerMore = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                   target:nil action:nil];
    nagetiveSpacerMore.width = 0;//这个值可以根据自己需要自己调整
    self.navigationItem.rightBarButtonItems = @[nagetiveSpacerMore, rightBarButtonItemsMore, rightBarButtonItems];

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!