Currently I'm trying to customize delte button in UITableViewCell.
Now I've got something like that:
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!
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With