导航栏添加多个右按钮且图片在上文字在下
//自定义导航栏多个右按钮
UIButton* myCollectionButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 45, 35)];
[myCollectionButton setTitle:@"我的收藏" forState:UIControlStateNormal];
myCollectionButton.titleLabel.font = [UIFont systemFontOfSize:11];
[myCollectionButton addTarget:self action:@selector(myCollectionAction:) forControlEvents:UIControlEventTouchUpInside];
UIImageView* myCollectionImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"我的收藏"]];
myCollectionImage.frame = CGRectMake(myCollectionButton.frame.size.width / 4, 0, 20, 20);
[myCollectionButton addSubview:myCollectionImage];
//调用方法
[self initButton:myCollectionButton];
UIBarButtonItem* checkBar = [[UIBarButtonItem alloc] initWithCustomView:checkButton];
UIBarButtonItem* secrchBar = [[UIBarButtonItem alloc] initWithCustomView:secrchButton];
UIBarButtonItem* myCollectionBar = [[UIBarButtonItem alloc] initWithCustomView:myCollectionButton];
//添加多个按钮(我只写了一个按钮,其余两个按钮相同设置)
self.navigationItem.rightBarButtonItems = @[secrchBar, myCollectionBar, checkBar];
self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
//使其按钮 文字与图片垂直显示
- (void)initButton:(UIButton*)button {
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;//使按钮靠下显示
[button setTitleEdgeInsets:UIEdgeInsetsMake(button.imageView.frame.size.height ,-button.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度,距离左边框减少imageView的宽度,距离下边框和右边框距离不变
[button setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0,0.0, -button.titleLabel.bounds.size.width)];//图片距离右边框距离减少图片的宽度,其它不边
}