参考代码:主要设置创建一个UIBarButtonSystemItemFixedSpace样式的UIBarButtonItem,然后设置间距,代码如下:
UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
searchBtn.frame = CGRectMake(0,0,21, 21);
//[searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
[searchBtn setBackgroundImage:[UIImage imageNamed:@"search_icon"] forState:UIControlStateNormal];
[searchBtn setBackgroundImage:[UIImage imageNamed:@"search_icon"] forState:UIControlStateHighlighted];
searchBtn.titleLabel.font = [UIFont systemFontOfSize:14];
[searchBtn addTarget:self action:@selector(searchBtnClick) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:searchBtn];
UIBarButtonItem *rightSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
rightSpace.width = -7;
self.navigationItem.rightBarButtonItems = @[rightSpace,rightItem];
网友评论