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];
来源:oschina
链接:https://my.oschina.net/u/2519763/blog/743287