iOS 10:UITableViewCell删除按钮自定义高度

时间:2017-09-21 07:41:36

标签: objective-c uitableview ios10

使用自定义UITableViewCell,我试图更改tableViewCell删除按钮的高度。我已经在SO上尝试了所有可用的解决方案。

每个人都提到在customTableViewCell类中我们需要覆盖layoutSubviews方法并迭代self.subViews以找到一个等于 UITableViewCellDeleteConfirmationView 的subView或其他iOS版本它是 UITableViewCellDeleteConfirmationControl 所以我使用了以下代码:

- (void)layoutSubviews
{
    [super layoutSubviews];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.0f];

    for (UIView *subView in self.subviews) {
        NSLog(@"subview: %@", self.subviews);
        if([NSStringFromClass([subView class]) rangeOfString:@"Delete"].location != NSNotFound) {
            CGRect newFrame = subView.frame;
            newFrame.size.height = 87;
            subView.frame = newFrame;
        }
    }

    [UIView commitAnimations];
}

self.subView只有两个观点,即

  • UITableViewCellContentView
  • UITableViewCellSeparatorView

如何在iOS 10 +中获取tableViewCell的删除按钮视图?

修改

这是我的视图层次结构:

enter image description here

4 个答案:

答案 0 :(得分:4)

对于任何正在努力解决同样问题的人。

iOS10 + 视图层次结构中,UITableView的删除按钮已更改。现在它属于UITableView - UISwipeActionPullView - UISwipeActionStandardButton

enter image description here

所以现在我们不需要重写自定义UITableViewCell的layoutSubviews方法,而是需要迭代UITableView子视图以获取UISwipeActionStandardButton。我发现tableView的willBeginEditingRowAtIndexPath委托是一个合适的地方,

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    for (UIView *subview in tableView.subviews) {
        if ([NSStringFromClass([subview class]) isEqualToString:@"UISwipeActionPullView"]) {
            if ([NSStringFromClass([subview.subviews[0] class]) isEqualToString:@"UISwipeActionStandardButton"]) {
                CGRect newFrame = subview.subviews[0].frame;
                newFrame.size.height = 72;
                subview.subviews[0].frame = newFrame;

                //Other changes on this view can also be applied like
                subview.subviews[0].backgroundColor = [UIColor redColor];
                subview.subviews[0].layer.cornerRadius = 12;
                subview.subviews[0].layer.masksToBounds = YES;
            }
        }
    }
}

答案 1 :(得分:1)

对于IOS 13,位置已再次更改,而不是在表视图内更改,它再次在_UITableViewCellSwipeContainerView中。因此,您也应该对其进行迭代。请在下面查看

   ([NSStringFromClass([subview class]) 
    isEqualToString:@"_UITableViewCellSwipeContainerView"]){

            for (UIView *deleteButtonSubview in subview.subviews){

                if ([NSStringFromClass([deleteButtonSubview class])
                     isEqualToString:@"UISwipeActionPullView"]) {



                    if ([NSStringFromClass([deleteButtonSubview.subviews[0] class]) isEqualToString:@"UISwipeActionStandardButton"]) {

                       //do what you want
                    }
                }

            }
        }

答案 2 :(得分:1)

Swift 5,适用于 iOS12、iOS13 和 iOS14

  func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
    // for iOS13, iOS14
    if let swipeContainerView = tableView.subviews.first(where: { String(describing: type(of: $0)) == "_UITableViewCellSwipeContainerView" }) {
      if let swipeActionPullView = swipeContainerView.subviews.first, String(describing: type(of: swipeActionPullView)) == "UISwipeActionPullView" {
        swipeActionPullView.frame.size.height -= 10
      }
    }

    // for iOS12
    tableView.subviews.forEach { subview in
      if String(describing: type(of: subview)) == "UISwipeActionPullView" {
        subview.frame.size.height -= 10
      }
    }
  }

答案 3 :(得分:0)

相同的代码,但可与Swift 5iOS 12+一起使用

 func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
        for subview in tableView.subviews {
            if NSStringFromClass(type(of: subview)) == "UISwipeActionPullView",
                let button = subview.subviews.first,
                NSStringFromClass(type(of: button)) == "UISwipeActionStandardButton" {

                button.backgroundColor = .red
                button.layer.cornerRadius = 8
                button.layer.masksToBounds = true
                (button as? UIButton)?.contentHorizontalAlignment = .center
                (button as? UIButton)?.contentVerticalAlignment = .center
                (button as? UIButton)?.titleEdgeInsets = UIEdgeInsets(top: 6, left: 0, bottom: 0, right: 0)
                button.superview?.backgroundColor = UIColor(white: 0.97, alpha: 1)
                button.superview?.layoutIfNeeded()
            }
        }
    }