简体   繁体   中英

custom size of UITableviewCell's delete button

I'm trying to resize UITableView cell's delete button with this code but for some reason, the x & y are working fine but i'm unable to change the height & width of the delete button. I'm using this code in my custom UITableViewCell class and everything works fine excel the width & hight of the "Delete" button. what am i missing here?

- (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];}

use this code...

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

see another answer from this link... iphone-uitableview-delete-button

Use this code in your custom Cell class

-(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;
}

Find the "UITableViewCellDeleteConfirmationView" in the UITableviewCell for changing the delete button height.

Use this code in your custom cell class

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

}
}

}

You can also do it using constraints ( working on iOS 8.x +). That way the animations (delete button animation especially) stay neat / no UI glitch.

In your UITableViewCell subclass :

Declare a weak property in your class anonymous category :

@property (weak, nonatomic) UIView *lastDeleteConfirmationView;

Disable translating of autoresizing mask constraints and add your own constraints :

- (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;
    }
}

SWIFT

override func layoutSubviews() in CustomTableViewCell: 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
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM