2

我正在尝试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];}
4

5 回答 5

3

使用此代码...

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

于 2013-04-26T05:12:29.383 回答
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;
}
于 2015-01-14T06:07:05.023 回答
1

在 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;

}
}

}

于 2016-02-19T07:15:31.093 回答
0

迅速

覆盖 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
        }
    }
}
于 2017-05-23T17:07:08.673 回答
0

您也可以使用约束(在 iOS 8.x + 上工作)来做到这一点。这样动画(尤其是删除按钮动画)保持整洁/没有 UI 故障。

在您的 UITableViewCell 子类中:

在您的类匿名类别中声明一个弱属性:

@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;
    }
}
于 2016-05-04T13:14:14.640 回答