- (void)addNavigationItem{
UIButton *btnOne = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
btnOne.backgroundColor = [UIColor redColor];
UIBarButtonItem *leftOne = [[UIBarButtonItem alloc] initWithCustomView:btnOne];
//UIBarButtonSystemItemFlexibleSpace 和 UIBarButtonSystemItemFixedSpace 两个是无样式的,在调用的过程中,使用 UIBarButtonSystemItemFlexibleSpace去占位,达到实现规范化的目的,相应的 UIBarButtonSystemItemFixedSpace则是使特定位置控件替代.
UIBarButtonItem *spaceBar = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
/**
* width为负数时,相当于btn向右移动width数值个像素,由于按钮本身和边界间距为5pix,所以width设为-5时,间距正好调整
* 为0;width为正数时,正好相反,相当于往左移动width数值个像素
*/
spaceBar.width = -15;
self.navigationItem.leftBarButtonItems = @[spaceBar,leftOne];
}
运行结果如图: