导航栏左右侧多个按钮间距设置

方法一:两个按钮中间添加一个弹簧样式的itemUIBarButtonSystemItemFixedSpace,设置宽度属性,可以增加两个按钮间距,设置为负数,可以减少两个按钮间距,但是效果不一定能达到要求

UIButton*button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
UIBarButtonItem *newItem=[[UIBarButtonItem alloc]initWithCustomView:bgview];
NSMutableArray *arrayItems=[self.navigationItem.leftBarButtonItems mutableCopy];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
// 在这里测试负数没有达到效果,正数是可以加大间距
negativeSpacer.width = -10;  
[arrayItems addObject:negativeSpacer];

方法二:设置button的x为负数,外边在套一个UIView

UIButton*button=[[UIButton alloc]initWithFrame:CGRectMake(-10, 0, 44, 44)];
UIView *bgview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
[bgview setBackgroundColor:[UIColor clearColor]];
[bgview  addSubview:button];
UIBarButtonItem*newItem=[[UIBarButtonItem alloc]initWithCustomView:bgview];
[arrayItems addObject:newItem];
self.navigationItem.leftBarButtonItems=[arrayItems copy];

你可能感兴趣的:(导航栏左右侧多个按钮间距设置)