我一直在使用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];
}
}