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

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

制約を使用して実行することもできます(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 に答える