調(diào)整navigationItem上leftBarButtonItem和rightBarButtonItem與屏幕邊界的間距
我們的項目中可能會碰見這樣的需求:需要我們設(shè)置navigationItem上leftBarButtonItem和rightBarButtonItem與屏幕邊界的間距黎比,可是這些不是系統(tǒng)都默認(rèn)設(shè)置好的嘛?我們能改嗎?
我們當(dāng)然可以更改忌堂,需要用到這個常量UIBarButtonSystemItemFixedSpace
**UIBarButtonSystemItemFixedSpace:指定固定間距值時强窖,使用 UIBarButtonSystemItemFixedSpace; UIBarButtonSystemItemFlexibleSpace: 能自動調(diào)節(jié)按鈕間的間距(這個我用的少惫皱,感覺沒有上面那個固定值的好用) **
UIView *leftBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH/2, 44)];
leftBarView.backgroundColor = [UIColor greenColor];
UIBarButtonItem *leftBtnItem = [[UIBarButtonItem alloc]initWithCustomView:leftBarView];
self.navigationItem.leftBarButtonItem = leftBtnItem;
//解決UIBarButtonItem偏移
UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSeperator.width = -20;
// 注意下面這個順序不要寫錯了夭问,寫錯了順序會導(dǎo)致寫不出我們想要的效果
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSeperator,leftBtnItem,nil];
UIView *right = [[UIView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH/3, 44)];
right.backgroundColor = [UIColor orangeColor];
UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc]initWithCustomView:right];
self.navigationItem.rightBarButtonItem = rightBtnItem;
//解決UIBarButtonItem偏移
UIBarButtonItem *negativeSeperator2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSeperator2.width = -20;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSeperator2,rightBtnItem,nil];
這樣就可以了
注意兩點:1是上面數(shù)組中的順序不要寫錯烛卧。2是-20的距離是筆者自己嘗試出來的佩微,正常的系統(tǒng)按鈕就是距離屏幕-20距離
來張效果圖
Simulator Screen Shot 2016年4月5日 15.51.25.png