2

I'm trying to achieve something like this:

enter image description here

I'm follwing this Question's answer: Change background color of Cancel button for action sheet

and so far using the answer I have achived this:

enter image description here

But this is only working upto IOS 11, When I run the app on IOS 12.2 emulator the cancel button didn't separate from other buttons.

Here you can see:

enter image description here

I wanted to ask is it a latest IOS bug or Am I missing something for latest IOS?

Here is my Code:

let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

    alertController.addAction(UIAlertAction(title: "Weekly View", style: .default, handler: nil))
    alertController.addAction(UIAlertAction(title: "Monthly View", style: .default, handler: nil))

    if let firstSubview = alertController.view.subviews.first, let alertContentView = firstSubview.subviews.first {
        for view in alertContentView.subviews {
            view.backgroundColor = .blue
        }
    }
    alertController.view.tintColor = UIColor(red: 1.0, green: 0.2, blue: 0.33, alpha: 1)

    let cancelButtonViewController = CancelButtonViewController()
    let cancelAction = UIAlertAction(title: "", style: .cancel, handler: nil)
    cancelAction.setValue(cancelButtonViewController, forKey: "contentViewController")

    alertController.addAction(cancelAction)

    present(alertController, animated: true, completion: nil)

And CancelButtonViewController code:

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = .green

    let buttonText = UILabel()
    buttonText.text = "Cancel"
    buttonText.font = UIFont(name: "AppleSDGothicNeo-Regular", size: 20)
    buttonText.translatesAutoresizingMaskIntoConstraints = false

    view.addSubview(buttonText)

    buttonText.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    buttonText.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true


}

And one thing more I'm also getting a Constraints error by default whenever I create AlertView with .actionSheet style. Maybe that could be the reason. I have done some research and found this post in this post they suggested to ignore this error.

Error:

enter image description here

I hope my question didn't irritate you. As I'm a noob in IOS programming. Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Faisal Shahzad
  • 87
  • 1
  • 12
  • 1
    FWIW, given that the spacing is correct in iOS 12.2 when you just `alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel))`, I think the issue is not `UIAlertController` bug, but rather the sleight of hand to attempt to customize the background color with `CancelButtonViewController`. Regarding the constraints error, I’m experiencing the same warning, as [have others](https://stackoverflow.com/questions/55405570/uialertcontroller-actionsheet-unable-to-simultaneously-satisfy-constraints). This constraints warning would appear to be a bug. – Rob Apr 04 '19 at 20:18
  • 1
    The problem with the method you are using is that it relies on the undocumented structure of a `UIAlertAction` and this runs the risk of changing without you knowing it which is what has happened with iOS12. It's not a bug in iOS it's the result of using this undocumented behaviour. – Upholder Of Truth Apr 04 '19 at 20:59
  • 1
    Please don't post pictures of text. Pictures can't be searched or referenced and they are harder to read (and post). Please replace the pictures of text with the actual text, copied and pasted. – rmaddy Apr 04 '19 at 21:07

0 Answers0