我一直在使用 iOS 7 和我的导航控制器之一,出于某种原因,我不得不使用自定义后退按钮(旁边带有后退箭头图像和文本)作为 leftBarButtonItem。现在,在 iOS 7 中,当屏幕中显示操作表或警报时,它会淡出左右栏项目按钮。但是对于我的自定义按钮,只有文本会淡出,而对于后退箭头按钮,我必须进行特殊处理。有没有这样做的直接方法:
这是我的代码:
添加自定义按钮(PS:由于某些原因,我必须使用自定义按钮):
- (void)showCustomBackButton {
UIButton *aCustomBackButton = [UIButton buttonWithType:101];
[aCustomBackButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[aCustomBackButton setTitle:@"Back" forState:UIControlStateNormal];
UIImage *backArrow = [UIImage imageNamed:@"backArrowImage"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:@"backArrow"];
[imageView setFrame:CGRectMake(kScreenOrigin, topMargin, backArrow.size.width, backArrow.size.height)];
[aCustomBackButton addSubview:imageView];
UIBarButtonItem *aNegativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
aNegativeSpacer.width = -8.0;
UIBarButtonItem *aLeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aCustomBackButton];
aLeftBarButtonItem.tag = 100;
self.navigationItem.leftBarButtonItems = @[aNegativeSpacer, aLeftBarButtonItem];
}
- (void)showCustomBackButtonFaded {
UIBarButtonItem *aBarButtonItem = [self.navigationItem.leftBarButtonItems lastObject];
if (aBarButtonItem && aBarButtonItem.tag == 100) {
UIButton *aCustomBackButton = [UIButton buttonWithType:101];
[aCustomBackButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[aCustomBackButton setTitle:@"Back" forState:UIControlStateNormal];
UIImage *backArrow = [UIImage imageNamed:@"fadedBackArrow"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:backArrow];
[imageView setFrame:CGRectMake(kScreenOrigin, topMargin, backArrow.size.width, backArrow.size.height)];
[aCustomBackButton addSubview:imageView];
UIBarButtonItem *aNegativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
aNegativeSpacer.width = -8;
UIBarButtonItem *aLeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aCustomBackButton];
self.navigationItem.leftBarButtonItems = @[aNegativeSpacer, aLeftBarButtonItem];
}
}