Iphone UITableviewCell的自定义大小';s删除按钮
我试图用此代码调整Iphone UITableviewCell的自定义大小';s删除按钮,iphone,ios,objective-c,uitableview,Iphone,Ios,Objective C,Uitableview,我试图用此代码调整UITableView单元格的删除按钮的大小,但由于某些原因,x&y工作正常,但我无法更改删除按钮的高度和宽度。我在我的自定义UITableViewCell类中使用了这段代码,并且一切都很好,超过了“删除”按钮的宽度和高度。 我错过了什么 - (void)layoutSubviews { [super layoutSubviews]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFro
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];}
使用此代码
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;
}
请参阅此链接中的另一个答案…在自定义单元格类中使用此代码
-(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;
}
- (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;
}
}
在UITableviewCell中找到“UITableViewCellDeleteConfirmationView”,以更改删除按钮的高度
在自定义单元格类中使用此代码
-(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;
}
- (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+)。 这样,动画(尤其是删除按钮动画)保持整洁/没有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;
}
}
SWIFT
覆盖CustomTableViewCell:UITableViewCell中的func layoutSubviews()
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
}
}
}
看看这个链接,使用约束和避免任何UI故障的解决方案:它也改变了单元格的高度。。当我的文本是动态的。。我该如何设置不幸的是,你不能依赖于类名,因为它在每个iOS中都不断变化。现在它的
UITableViewCellDeleteConfirmationView
。