在iOS7之后,我们在设置UINavigationItem的leftBarButtonItem,rightBarButtonItem的时候都会造成位置的偏移,多添加一个消极的宽度为负值的UIBarButtonItem
UIButton *leftBtn = [[UIButtonalloc] initWithFrame:CGRectMake(0,0, 44,44)];
leftBtn.backgroundColor = [UIColorcyanColor];
[leftBtn setImage:[UIImageimageNamed:@"back.png"]forState:UIControlStateNormal];
[leftBtn addTarget:selfaction:@selector(back:)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftItem = [[UIBarButtonItemalloc] initWithCustomView:leftBtn];
UIBarButtonItem *ItemSpace = [[UIBarButtonItemalloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
if (MODEL_VERSION >=7.0) {
ItemSpace.width = -10;
}
self.navigationItem.leftBarButtonItems =@[ItemSpace, leftItem];
这种方法在IOS11 竟然不可用了,尝试了一下其他方法,目前看来是可行的.
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:imgName]
style:UIBarButtonItemStylePlain
target:self
action:@selector(btnRightClicked:)];
rightItem.tintColor=RGB333333;
[rightItemsetTitle:title];
NSDictionary*attributesDic =@{
NSFontAttributeName:[UIFontboldSystemFontOfSize:18],
NSForegroundColorAttributeName:RGB333333,
};
[rightItemsetTitleTextAttributes:attributesDic forState:UIControlStateNormal];
self.navigationItem.backBarButtonItem = nil;
self.navigationItem.rightBarButtonItems = [self combineWithMarginItem:-10 withItem:rightItem];
- (NSArray*)combineWithMarginItem:(NSInteger)offset withItem:(UIBarButtonItem*)item{
return@[item];
}
网友评论