Change the color of default red color delete button in UITableViewCell when swiping rows or click on edit button

I wanted to change the color of minus button and delete button of UITableViewCell when click on edit button or swiping UITableView rows. I have implemented this code so far :

-(IBAction)doEdit:(id)sender
{

    [[self keyWordsTable] setEditing:YES animated:NO];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

}

  • Animation with constraints in UITableViewCell launches incorrectly
  • Add filter button to UISearchController
  • where to create autolayout constraints for subclassed uitableviewcell?
  • Square instead of rounded corners in UITableViewCell grouped style
  • How to implement table sections with core data
  • Xcode: Passing Information from UITableViewController to UIViewController
  • 5 Solutions Collect From Internet About “Change the color of default red color delete button in UITableViewCell when swiping rows or click on edit button”


    iOS 8 and 9 (props to this post)


    Note: If you are working with an existing iOS 7 project, you’ll need to update the target to iOS 8 to get this functionality. Also remember to set the UITableviewDelegate.

    All the magic now happens here (as many buttons as you want too!!!!):

     -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
     UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
        {
            NSLog(@"Action to perform with Button 1");
        }];
        button.backgroundColor = [UIColor greenColor]; //arbitrary color
        UITableViewRowAction *button2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 2" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                        {
                                            NSLog(@"Action to perform with Button2!");
                                        }];
        button2.backgroundColor = [UIColor blueColor]; //arbitrary color
    
        return @[button, button2];
    }
    
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // you need to implement this method too or nothing will work:
    
    }
     - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
        {
            return YES;
        }
    


    (iOS 7)


    **activate the delete button on swipe**
    
    // make sure you have the following methods in the uitableviewcontroller
    
        - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
        {
            return YES;
        }
        - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
        {
            NSLog(@"You hit the delete button.");
        }
    

    set custom text label instead of delete.

    -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return @"Your Label";
    }
    

    set custom color for button part 1 – warning, this technically involves poking at the private apple API. However, you are not prevented from modifying a subview using a public method search that is part of UIKIT.

    Create a uitableviewcell class (see also https://stackoverflow.com/a/22350817/1758337 )

    - (void)layoutSubviews
    {
        [super layoutSubviews];
        for (UIView *subview in self.subviews) {
            //iterate through subviews until you find the right one...
            for(UIView *subview2 in subview.subviews){
                if ([NSStringFromClass([subview2 class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
                    //your color
                    ((UIView*)[subview2.subviews firstObject]).backgroundColor=[UIColor blueColor];
                }
            }
        }    
    }
    

    Another note: there’s no guarantee this approach will work in future updates. Also beware that mentioning or using the private UITableViewCellDeleteConfirmationView class may lead to AppStore rejection.

    set custom color for button part 2

    back in your uitableviewcontroller

    - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [YourTableView reloadData];
    }
    

    (The alternate color won’t be called until the next time layoutSubviews is called on the tablecell, so we ensure this happens by reloading everything.)


    Swift Example (iOS 8)

    UITableViewDelegate docs (editActionsForRowAtIndexPath method)

    Return Value

    An array of UITableViewRowAction objects representing the actions for
    the row. Each action you provide is used to create a button that the
    user can tap.

    Discussion

    Use this method when you want to provide custom actions for
    one of your table rows. When the user swipes horizontally in a row,
    the table view moves the row content aside to reveal your actions.
    Tapping one of the action buttons executes the handler block stored
    with the action object.

    If you do not implement this method, the table view displays the
    standard accessory buttons when the user swipes the row.

    Working example in Swift:

    @available(iOS 8.0, *)
    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
        let button1 = UITableViewRowAction(style: .Default, title: "Happy!") { action, indexPath in
            print("button1 pressed!")
        }
        button1.backgroundColor = UIColor.blueColor()
        let button2 = UITableViewRowAction(style: .Default, title: "Exuberant!") { action, indexPath in
            print("button2 pressed!")
        }
        button2.backgroundColor = UIColor.redColor()
        return [button1, button2]
    }
    
    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    }
    

    fist time you call willTransitionToState in .m (customcell)

    - (void)willTransitionToState:(UITableViewCellStateMask)state{
        NSLog(@"EventTableCell willTransitionToState");
        [super willTransitionToState:state];
        [self overrideConfirmationButtonColor];
    }
    

    Check version iOS, it’s here, i’m using iOS 7 – iOS8

    //at least iOS 8 code here
    - (UIView*)recursivelyFindConfirmationButtonInView:(UIView*)view
    {
        if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) {
            // iOS 8+ code here
            for(UIView *subview in view.subviews) {
    
                if([NSStringFromClass([subview class]) rangeOfString:@"UITableViewCellActionButton"].location != NSNotFound)
                    return subview;
    
                UIView *recursiveResult = [self recursivelyFindConfirmationButtonInView:subview];
                if(recursiveResult)
                    return recursiveResult;
            }
        }
    
        else{
            // Pre iOS 8 code here
            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)
            {
                UIColor *color = UIColorFromRGB(0xFF7373);
                confirmationButton.backgroundColor = color;
    
            }
        });
    }
    

    Not possible using public API.

    For the delete button, you can use a custom implementation, such as SWTableViewCell, to change the color of the button, as well as add others.

    Old Question, but am sure there are some people who support iOS 7.
    To change the delete button background colour, you need to create “UITableViewCell” class or extend it.
    then you can use

    - (void)layoutSubviews
    {
        [super layoutSubviews];
        for (UIView *subview in self.subviews) {
            for(UIView *childView in subview.subviews){
                if ([childView isKindOfClass:[UIButton class]]) {
                    childView.backgroundColor = [UIColor blueColor];
                }
            }
        }
    }