Properly using backgroundEffect on UITableViewRowAction

499 Views Asked by At

I am trying to get my UITableViewRowAction options (table view slide options) to change their background color when I press down on them. I know this is possible because the native Mail app & (3rd party) Facebook Messenger uses it as well.

Here is my code:

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
    {
        //First button -- Follow
        let follow = UITableViewRowAction(style: .Normal, title: "Follow", handler:{ action, index in
            print("follow button tapped")
        })
        //set the color of the first button to yellow
        follow.backgroundColor = UIColor(red: 255/255, green: 216/255, blue: 0/255, alpha: 1)
        //***follow.backgroundEffect = UIBlurEffect(style: UIBlurEffect.Dark)***

        //Second button -- Post
        let post = UITableViewRowAction(style: .Normal, title: " Post  ", handler:{ action, index in
            print("post button tapped")
        })
        //set the color of the second button to light blue
        post.backgroundColor = UIColor(red: 0/255, green: 175/255, blue: 255/255, alpha: 1)

        //Third button -- Map
        let map = UITableViewRowAction(style: .Normal, title: "Map    ", handler:{ action, index in
            print("map button tapped")
        })
        //set the color of the third button to red
        map.backgroundColor = UIColor(red: 255/255, green: 75/255, blue: 75/255, alpha: 1)


        return [follow, post, map]
    }

From another post I see that the only properties you are able to change for UITableViewRowActions are:

  1. BackgroundEffect
  2. Background color
  3. Title
  4. Style

I thought I could make this work by changing the color of the pressed option in the handler but this didn't work. When I try to change the background color in the handler I get the error:

"Variable used within its own initial value"

I attempted to change the BackgroundEffect property--you can see it commented out in my code-- but was unable to do so successfully.

0

There are 0 best solutions below