IOS7下用系統的UIBarButtonItem時傳回按鈕左邊距比IOS6小,見下圖:
圖1:上面是IOS6,下面是IOS7
需求:
IOS6和IOS7的傳回按鈕要統一成IOS7樣式的,即偏移量要像下面那個圖那麼寬,那IOS6怎麼辦呢?
解決方案:
#define IOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
+ (UIBarButtonItem *)backItem
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:STR_button_backItem]
forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
CGFloat offset = 8;
if (IOS7) {
offset = 0;
}
button.frame = CGRectMake(offset, 0, kBackButtonWidth, kBackButtonHeight);
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBackButtonWidth+offset, kBackButtonHeight)];
button.tag = 147;
[aView addSubview:button];
aView.userInteractionEnabled = YES;
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:aView];
return backItem;
}
- (void)setTarget:(id)target action:(SEL)action
{
UIButton *buttom = (UIButton *)[(self.customView) viewWithTag:147];
[buttom addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}
//設定導航欄傳回按鈕
UIBarButtonItem *backButton = [UIBarButtonItem backItem];
[backButton setTarget:self action:@selector(backButtonItemClick)];
self.navigationItem.leftBarButtonItem = backButton;
希望對大家有幫助~!~!