UIBarButtonItem按钮之间系统自带的间隙就比较合理,美工切图不要留白就好。
一、设置UIEdgeInsets
如果是通过图片创建的UIBarButtonItem, 我们可以通过设置imageInsets属性来调整image到按钮的距离, 来缩小间距:
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed: @"gouwuche.png"] style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search.png"] style:UIBarButtonItemStylePlain target:nil action:nil];
//设置图片与按钮间距
[item2 setImageInsets:UIEdgeInsetsMake(0, 10, 0, -10)];
self.navigationItem.rightBarButtonItems = @[item1,item2];
二、使用UIBarButtonSystemItemFixedSpace
创建一个type为UIBarButtonSystemItemFixedSpace的UIBarButtonItem进行占位, 并将其设置为负值. 此方法只对调整UIBarButtonItem 到左右边距有效.对多个按钮间间距无效
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"gouwuche.png"] style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space.width = -10;
self.navigationItem.rightBarButtonItems = @[space,item];