0

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 ?

Louiza A
  • 23
  • 6
  • Does this answer your question? [How to change UIAlertController button text colour in iOS9?](https://stackoverflow.com/questions/32856892/how-to-change-uialertcontroller-button-text-colour-in-ios9) – stackich Jun 01 '23 at 06:55
  • @stackich thanks for you comment but I want to change the background colour of the cancel action, not the text colour. – Louiza A Jun 01 '23 at 07:09
  • try using the same approach with different key in `forKey` parameter. – stackich Jun 01 '23 at 07:14
  • @stackich okay, do you know what is the key to change the background colour? – Louiza A Jun 01 '23 at 07:20
  • I don't know, you can try to find it, I am just giving suggestions. If there is no one, I believe that you cannot change the background color. However, you should not change Apple's standard, I have been using iPhone for years and I have never seen a different color of the text and especially of the background on buttons in Alert/ActionSheet. – stackich Jun 01 '23 at 07:29
  • @stackich I only want to have the same "grey" background colour for all my actions and for now for some reason the cancel action has white background colour... – Louiza A Jun 01 '23 at 07:34
  • It seems like iOS always does this for action sheets. However, it is not the cancel style action that always has the white background, it is the *preferred* action. The cancel style action will be the preferred action unless one of the other actions is marked as the preferred action using https://developer.apple.com/documentation/uikit/uialertcontroller/1620102-preferredaction – Geoff Hackworth Jun 01 '23 at 08:07
  • @GeoffHackworth thanks for your solution. I tried it but nothing seems to changed. Again, the cancel style action has white background even after I set the default action as the preferred action of the alert. – Louiza A Jun 01 '23 at 08:16
  • I didn’t say it would change, I just pointed out that it is the preferred action rather than the cancel action that is highlighted (although they are often the same). It is shown in a separate block with bold text and a lighter background. This is just the way iOS is designed to work: https://developer.apple.com/design/human-interface-guidelines/action-sheets – Geoff Hackworth Jun 01 '23 at 08:31
  • @GeoffHackworth you have a point and thank you for the explanation. But until now, I can't find a solution. If you have any idea to help me, please share it. – Louiza A Jun 08 '23 at 07:43

0 Answers0