iOS 7:如何为UIControlStateHighlighted设置UIBarButtonItem backButtonBackgroundImage?

前端之家收集整理的这篇文章主要介绍了iOS 7:如何为UIControlStateHighlighted设置UIBarButtonItem backButtonBackgroundImage?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在设置正常和突出显示状态下的后退按钮的背景图像.
- (void)configureBackButtonInNavigationItem:(UINavigationItem *)item
{
    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back"
            style:UIBarButtonItemStyleBordered target:nil action:NULL];
    [backBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]} forState:UIControlStateNormal];
    [backBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateHighlighted];

    // white arrow image
    UIImage *normalImage = [[[UIImage imageNamed:@"btn_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] resizableImageWithCapInsets:UIEdgeInsetsMake(0.f,17.f,0.f,0.f)];

    // orange arrow image
    UIImage *pressedImage = [[[UIImage imageNamed:@"btn_on_press"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] resizableImageWithCapInsets:UIEdgeInsetsMake(0.f,0.f)];

    [backBarButtonItem setBackButtonBackgroundImage:normalImage
                    forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [backBarButtonItem setBackButtonBackgroundImage:pressedImage
                forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

    [backBarButtonItem setBackgroundImage:normalImage
                    forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [backBarButtonItem setBackgroundImage:pressedImage
                forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

    NSLog(@"NORMAL: %@ HIGHLIGHTED: %@",[backBarButtonItem backButtonBackgroundImageForState:UIControlStateNormal barMetrics:UIBarMetricsDefault],[backBarButtonItem backButtonBackgroundImageForState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault]);
    item.backBarButtonItem = backBarButtonItem;

    NSLog(@"NORMAL: %@ HIGHLIGHTED: %@",[backBarButtonItem backButtonBackgroundImageForState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault]);
}

输出如下:

NORMAL: <_UIResizableImage: 0x16b55e10> HIGHLIGHTED: <_UIResizableImage: 0x16b593d0>
NORMAL: <_UIResizableImage: 0x16b55e10> HIGHLIGHTED: <_UIResizableImage: 0x16b593d0>

但是,突出显示状态的观察结果只是将设置为正常状态的调光,而不是使用正确的突出显示的图像.

正常:

突出显示(箭头仍然为白色,按钮意外变暗):

请不要发布关于使用leftBarButtonItem或UIButton作为自定义视图的答案.这两种方法都可以在iOS 7上制作滑动向后的行为.

UPD:关于这个问题的填充雷达#17481106.

UPD2:雷达#17481106在iOS 8中修复.

解决方法

目前苹果在InteractivePopGestureRecognizer上存在错误(这样可以在推动动画后滑动导航控制器的视图,您将看到嵌套的流行动画可能会导致控制台中的导航栏警告被损坏),顺便说一下,我们可以让一个小黑客工作围绕这个bug.

这是一个适用于我的解决方案,

将NavigationController类子类化并使其委托该手势

@interface CBNavigationController : UINavigationController 
@end

@implementation CBNavigationController

- (void)viewDidLoad
{
  __weak CBNavigationController *weakSelf = self;

  if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
  {
    self.interactivePopGestureRecognizer.delegate = weakSelf;
    self.delegate = weakSelf;
  }
}

// Hijack the push method to disable the gesture

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
  if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
    self.interactivePopGestureRecognizer.enabled = NO;

  [super pushViewController:viewController animated:animated];
}

#pragma mark UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animate
{
  // Enable the gesture again once the new controller is shown

  if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
    self.interactivePopGestureRecognizer.enabled = YES;
}


@end

用户在转换过程中开始向后滑动时,弹出事件会堆积并导致导航堆栈“损坏”.我的解决方法是在推送转换期间临时禁用手势识别器,并在新的视图控制器加载时再次启用.再次,使用UINavigationController子类更容易.

之后,您可以冷静地使用item.leftBarButtonItem和UIButton作为自定义视图.

原文链接:https://www.f2er.com/iOS/336583.html

猜你在找的iOS相关文章