Different background colour on Cancel action of UIAlertController with actionSheet style

49 Views Asked by At

I have a UIAlertController with preferred style actionSheet and a cancel action with cancel style. The issue I have is that as you can see from the image below, the background colour of the cancel action is slightly different from the other actions.

enter image description here

This is the piece of code I have:

   func showAlertWithThreeButtons(alertTitle: String, message: String, defaultTitle1: String, defaultTitle2: String, cancelTitle: String) {
    let alert = UIAlertController(title: alertTitle, message: message, preferredStyle: .actionSheet)
    
    let defaultAction = UIAlertAction(title: defaultTitle1, style: .default, handler: { (_) in
        self.baseDelegate?.defaultButtonPressed()
    })
    
    let default2Action = UIAlertAction(title: defaultTitle2, style: .default, handler: { (_) in
        self.baseDelegate?.default2ButtonPressed()
    })
    
    let cancelAction = UIAlertAction(title: cancelTitle, style: .cancel, handler: { (_) in
        self.baseDelegate?.cancelButtonPressed()
    })
    
    alert.setValue(NSAttributedString(string: alert.title!, attributes: [NSAttributedString.Key.font : UIFont.init(name: "SF Compact Display", size: 18) ?? UIFont.systemFont(ofSize: 18.0, weight: .regular), NSAttributedString.Key.foregroundColor : UIColor(named: "Grey#444444") ?? UIColor(red: 68, green: 68, blue: 68, alpha: 1)]), forKey: "attributedTitle")
    alert.setValue(NSAttributedString(string: alert.message!, attributes: [NSAttributedString.Key.font : UIFont.init(name: "SF Compact Display", size: 13) ?? UIFont.systemFont(ofSize: 13.0, weight: .regular), NSAttributedString.Key.foregroundColor : UIColor(named: "Grey#444444") ?? UIColor(red: 68, green: 68, blue: 68, alpha: 1)]), forKey: "attributedMessage")
    
    defaultAction.setValue(UIColor(named: "Blue#007AFF"), forKey: "titleTextColor")
    default2Action.setValue(UIColor(named: "Grey#444444"), forKey: "titleTextColor")
    cancelAction.setValue(UIColor(named: "Grey#444444"), forKey: "titleTextColor")
    
    alert.addAction(defaultAction)
    alert.addAction(default2Action)
    alert.addAction(cancelAction)        
    
    self.present(alert, animated: true, completion: nil)
}

I have run the app on simulator and a device and the result is the same. Do you know how I can have the same background colour on all of my actions ?

0

There are 0 best solutions below