iOS+调整导航按钮间隔

自定义导航左/右按钮的时候,由于图片或其他原因总导致按钮的边距过大且不好调整,如果再添加一个UIBarButtonSystemItemFixedSpace的item就很好解决了.

// 1.自定义返回按钮
UIImage *backIcon = [UIImage imageNamed:@"back_icon"];
UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(-20, 0, 30, 30)];
[backButton setImage:backIcon forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backToPrevious) forControlEvents:UIControlEventTouchUpInside];

// 2.添加UIBarButtonSystemItemFixedSpace类型的item
UIBarButtonItem *leftNegativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
// 2.1-----重点:通过调整width值来调整间距----
leftNegativeSpacer.width = -15;


// 3.添加
NSArray *leftItems = @[leftNegativeSpacer,[[UIBarButtonItem alloc]initWithCustomView:backButton]];
self.navigationItem.leftBarButtonItems = leftItems;

// 4.如果是两按钮之间需要调整间距也可以加上一个UIBarButtonSystemItemFixedSpace类型的item
// eg:Item1和Item2之间有个间距 -->  @[Item1,FixedSpaceItem,Item2];

你可能感兴趣的:(iOS+调整导航按钮间隔)