【问题标题】:custom size of UITableviewCell's delete buttonUITableviewCell 删除按钮的自定义大小
【发布时间】:2013-04-26 04:45:30
【问题描述】:

我正在尝试使用此代码调整UITableView 单元格的删除按钮的大小,但由于某种原因,x 和 y 工作正常,但我无法更改删除按钮的高度和宽度。我在我的自定义 UITableViewCell 类中使用此代码,一切正常,超出“删除”按钮的宽度和高度。 我在这里错过了什么?

- (void)layoutSubviews
{
[super layoutSubviews];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.0f];

for (UIView *subview in self.subviews) {
    if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {

        CGRect newFrame = subview.frame;
        newFrame.origin.x = 250;
        newFrame.origin.y = 47;
        newFrame.size.height = 30;
        newFrame.size.width = 50;

        deleteButtonView.frame = newFrame;
        subview.frame = newFrame;
    }
}
[UIView commitAnimations];}

【问题讨论】:

标签: iphone ios objective-c uitableview


【解决方案1】:

使用此代码...

if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
      UIView *deleteButtonView = (UIView *)[subview.subviews objectAtIndex:0];
      CGRect f = deleteButtonView.frame;
      f.origin.x = 250;
      f.origin.y = 47;
      f.size.width = 30;
      f.size.height = 50;

      CGRect sf = self.frame;
      sf.size.width = 100;
      sf.size.height = 100;

      deleteButtonView.frame = f;
      self.frame = sf;
}

从此链接查看另一个答案...iphone-uitableview-delete-button

【讨论】:

  • 不幸的是,你不能依赖类名,因为它会随着每个 iOS 不断变化。现在是UITableViewCellDeleteConfirmationView
【解决方案2】:

在您的自定义 Cell 类中使用此代码

-(void)layoutSubviews
{
  NSMutableArray *subviews = [self.subviews mutableCopy];
  UIView *subV = subviews[0];
  [subviews removeObjectAtIndex:0];
  CGRect f = subV.frame;
  f.size.height = 70; // Here you set height of Delete button
  subV.frame = f;
}

【讨论】:

  • 它也改变了单元格的高度。
  • 它正在工作但是..当我的文本是动态的..我该如何设置这个
【解决方案3】:

在 UITableviewCell 中找到“UITableViewCellDeleteConfirmationView”,用于更改删除按钮的高度。

在您的自定义单元类中使用此代码

 - (void)layoutSubviews {
[super layoutSubviews];
NSMutableArray *subviews = [self.subviews mutableCopy];
while (subviews.count > 0)
{
    UIView *subV = subviews[0];
    [subviews removeObjectAtIndex:0];

    if ([NSStringFromClass([subV class])isEqualToString:@"UITableViewCellDeleteConfirmationView"])//Here you select the subView of delete button {
    UIView *deleteButtonView = (UIView *)[self.subviews objectAtIndex:0];


    CGRect buttonFrame = deleteButtonView.frame;
    buttonFrame.origin.x = deleteButtonView.frame.origin.x;
    buttonFrame.origin.y = deleteButtonView.frame.origin.y;
    buttonFrame.size.width = deleteButtonView.frame.size.width;
        buttonFrame.size.height = 70;  //Here you set the height
    deleteButtonView.frame = buttonFrame;

}
}

}

【讨论】:

    【解决方案4】:

    您也可以使用 constraints 来实现(适用于 iOS 8.x +)。 这样动画(尤其是删除按钮动画)保持整洁/没有 UI 故障。

    在你的 UITableViewCell 子类中:

    在你的类匿名类别中声明一个 weak 属性:

    @property (weak, nonatomic) UIView *lastDeleteConfirmationView;
    

    禁用自动调整掩码约束的翻译并添加您自己的约束:

    - (void)layoutSubviews {
        [super layoutSubviews];
    
        [self addConstraintsToCellDeleteConfirmationView];
    }
    
    - (void)addConstraintsToCellDeleteConfirmationView {
        UIView *deleteConfirmationView = nil;
        for (UIView *subview in self.subviews) {
            if ([NSStringFromClass(subview.class) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
                deleteConfirmationView = subview;
                break;
            }
        }
    
        if (deleteConfirmationView && self.lastDeleteConfirmationView != deleteConfirmationView) {
            self.lastDeleteConfirmationView = deleteConfirmationView;
            self.lastDeleteConfirmationView.translatesAutoresizingMaskIntoConstraints = NO;
    
            [NSLayoutConstraint constraintWithItem:self.customBackgroundView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.lastDeleteConfirmationView attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f].active = YES;
    
            [NSLayoutConstraint constraintWithItem:self.customBackgroundView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.lastDeleteConfirmationView attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0.0f].active = YES;
    
            [NSLayoutConstraint constraintWithItem:self.lastDeleteConfirmationView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.lastDeleteConfirmationView.superview attribute:NSLayoutAttributeRight multiplier:1.0f constant:0.0f].active = YES;
        }
    }
    

    【讨论】:

      【解决方案5】:

      SWIFT

      在 CustomTableViewCell 中覆盖 func layoutSubviews():UITableViewCell

      override func layoutSubviews() {
      
          for subview in self.subviews {
      
              if String(describing: type(of: subview.self)).isEqualToString("UITableViewCellDeleteConfirmationView") {
      
                  var newFrame = subview.frame
                  newFrame.origin.y = 10
                  newFrame.size.height = 60
                  subview.frame = newFrame
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-02
        • 1970-01-01
        • 2014-04-11
        • 1970-01-01
        • 1970-01-01
        • 2010-12-10
        • 1970-01-01
        相关资源
        最近更新 更多