如何在iOS 11上的UITableViewCell上进行自定义删除按钮

时间:2017-09-22 09:16:41

标签: xcode uitableview

这是可以出现UITableViewCell删除按钮的代码,带有一些自定义的东西(颜色,图像)但是当我在iOS 11上更新这个代码不起作用而不是我的自定义按钮我只有红色按钮。我怎么理解苹果改变关键“UITableViewCellDeleteConfirmationButton”再次。也许你知道如何解决这个问题?

- (UIView*)recursivelyFindConfirmationButtonInView:(UIView*)view
{
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) {
        // iOS 8+ code here
        for(UIView *subview in view.subviews) {

            if([NSStringFromClass([subview class]) rangeOfString:@"UITableViewCellActionButton"].location != NSNotFound)
                return subview;

            UIView *recursiveResult = [self recursivelyFindConfirmationButtonInView:subview];
            if(recursiveResult)
                return recursiveResult;
        }
    }

    else{
        // Pre iOS 8 code here
        for(UIView *subview in view.subviews) {
            if([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationButton"]) return subview;
            UIView *recursiveResult = [self recursivelyFindConfirmationButtonInView:subview];

            if(recursiveResult) return recursiveResult;

        }
    }
    return nil;

}

-(void)overrideConfirmationButtonColor
{

    dispatch_async(dispatch_get_main_queue(), ^{
        UIView *confirmationButton = [self recursivelyFindConfirmationButtonInView:self];
        if(confirmationButton)
        {
            self.buttonConfirm = [[UIButton alloc]initWithFrame:CGRectMake(13, 15, 90, 90)];
            self.buttonConfirmAnimation = [[UIImageView alloc]initWithFrame:CGRectMake(12, 15, 40, 40)];


            self.buttonConfirmAnimation.image = [UIImage imageNamed:@"heartWh"];

            confirmationButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"patternPlist@2x"]];

            [confirmationButton addSubview:self.buttonConfirmAnimation];

            [confirmationButton bringSubviewToFront:self.buttonConfirm];

        }
    });

}

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题 - 因为iOS 11.x所提议的解决方案不再适用。我注意到Apple不再使用UIButton作为删除按钮,而是使用名为UITableViewCellEditControl的UIView,并且此视图有两个子视图。

这两个子视图实际上是UIImageView类型,一个带有删除图标的彩色图像,另一个带有灰色版本(我想某种效果/阴影)。您仍然可以修改这些图片,例如设置他们的tintColors或完全改变图像。

不幸的是,现在还有两个问题:

  1. UITableViewCellEditControl也用于编辑样式为none,即它可能是选择控件(空点/圆圈)。在不使用UITableView本身的实际editStyle的情况下,两个用户似乎无法区分这两个用例。

  2. 点击删除控件后,它的图像将被Apple的原始图像替换。使用/设置UIImageView的突出显示图像属性也没有帮助。似乎UITableViewCellEditControl中存在一些潜在的逻辑,它会隐式地将图像重置为原始图像。