UITableviewCell的删除button的自定义大小
我试图调整UITableView
单元格的删除button的代码,但由于某种原因,X&Y工作正常,但我无法更改删除button的高度和宽度。 我在我的自定义UITableViewCell
类中使用此代码,一切工作正常优于“删除”button的宽度和高度。 我在这里错过了什么?
- (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];}
使用这个代码…
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
在您的自定义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; }
在UITableviewCell中find“UITableViewCellDeleteConfirmationView”来更改删除button的高度。
在您的自定义单元类中使用此代码
- (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; } }
}
你也可以使用约束 (在iOS 8.x +上工作)。 这样的animation(特别是删除buttonanimation)保持整洁/没有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; } }
迅速
在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 } } }