关于UIContextualAction的Image

UIContextualAction,iOS11引入,用于自定tableViewCell的action按钮,可以设置image,然而它会把你的image变成白色,就好像是UIImageView加了white的tintColor,并且设置RenderingMode无效...

一番尝试后的解决办法:

在tableView的layoutSubViews里面找到action的视图进行调整:

#define k_ash_action_img [@"__action" hash]

- (void)layoutSubviews {
   [super layoutSubviews];
   
   if (@available(iOS 11.0, *)) {
       if (self.editing)
       for (UIView *swipeActionPullView in self.subviews)
       {
           if([swipeActionPullView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]){
               for (UIView *swipeActionStandardButton in swipeActionPullView.subviews) {
                   if ([swipeActionStandardButton isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
                       
                       for (UIImageView *imageView in swipeActionStandardButton.subviews) {
                           if ([imageView isKindOfClass:[UIImageView class]]) {
                               if ([imageView viewWithTag:k_ash_action_img]==nil) {
                                   UIImageView *addedImageView = [[UIImageView alloc] initWithFrame:imageView.bounds];
                                   addedImageView.tag = k_ash_action_img;
                                   addedImageView.image= [imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
                                   [imageView addSubview:addedImageView];
                               }
                               break;
                           }
                       }
                   }
               } 
           }
       }
   }
}

注意:
iOS11 cell的willTransitionToState方法并不会每次action出来的时候都被调用,iOS10及以下没有问题,而iOS11这里写调整的代码只会起一次作用(可能是Xcode9的bug = =)。iOS10及以下在willTransitionToState的时候调整即可。

你可能感兴趣的:(关于UIContextualAction的Image)