Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the color of UITableViewCellDeleteConfirmationControl(Delete Button) in UITableViewCell

Currently I'm trying to customize delte button in UITableViewCell. Now I've got something like that:

enter image description here

Now, all what I need is to change the color of this button, I don't want to change the behavior, or to make it absolutelly custom. I'm sure that it is possible, without creating your own controls for deleting rows in UITableView.

This my code:

- (void)layoutSubviews
{
    [super layoutSubviews];

    for (UIView *subview in self.subviews) {
        if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
            UIView *deleteButtonView = (UIView *)[subview.subviews objectAtIndex:0];
            deleteButtonView.backgroundColor = [UIColor greenColor];
        }
    }
}

How could I do this?

Thanks in advance!

like image 789
Anatoliy Gatt Avatar asked Dec 07 '22 12:12

Anatoliy Gatt


2 Answers

UITableViewCellDeleteConfirmationControl is not a public class and you cannot easily change its appearance.

In order to do so I believe you'd have to iterate through the subviews of the cell and change its properties:

for (UIView *subview in self.subviews) {
    if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl") {
        // Set the background using an image.
    }
}

Of course, you should be wary of doing this sort of thing since its rather fragile. I'd suggest rolling your own instead.

I'd suggest submitting a bug report to Apple to request the ability to edit this more easily.

like image 60
tgt Avatar answered May 24 '23 08:05

tgt


In the UITableViewCell subclass:

- (UIView*)recursivelyFindConfirmationButtonInView:(UIView*)view
{
    for(UIView *subview in view.subviews) {
        if([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationButton"]) return subview;
        UIView *recursiveResult = [self recursivelyFindConfirmationButtonInView:subview];
        if(recursiveResult) return recursiveResult;
    }
    return nil;
}

-(void)overrideConfirmationButtonColor
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIView *confirmationButton = [self recursivelyFindConfirmationButtonInView:self];
        if(confirmationButton) confirmationButton.backgroundColor = [UIColor orangeColor];
    });
}

Then in the UITableViewDelegate:

-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    <#Your cell class#> *cell = (<#Your cell class#>*)[self.tableView cellForRowAtIndexPath:indexPath];
    [cell overrideConfirmationButtonColor];
}

This works in iOS 7.1.2

like image 32
silyevsk Avatar answered May 24 '23 07:05

silyevsk