您也可以使用约束(在 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;
}
}