UIAlertController foreground is dimmed when it should not be

I have a strange issue which is affecting all of many UIAlertControllers shown throughout my app - the AlertControllers' white foreground appears dimmed, or less bright, than it should be.

The easiest way to describe it is to illustrate the desired and expected effect I obtain by placing the relevant code in a fresh 'Test' Single View application (Swift 3 - I should note that the actual app is a Single View application using Swift 2.3).

In my 'Test' app, the code below uses a deprecated AlertView to show a sample message, then builds a AlertController and shows that immediately 'after'. There is no need to use the AlertView except to highlight the contrast in foreground 'white' - the problematic behaviour is the same if I comment out the AlertView.

Problematic behaviour: 4 images comparing alertcontroller in Test app vs real app

Image 1: Test app: the AlertView is shown and the AlertController then shows behind it. Note the whiteness/brightness of the AlertView foreground.

Image 2: Test app: when the AlertView is dismissed, the AlertController is then the uppermost viewcontroller and as expected and desired, its foreground brightness is exactly the same as the AlertView's previously was

Image 3: real app: same code, also positioned in viewDidAppear - the AlertView is shown with the expected whiteness/brightness

Image 4: real app: when the AlertView is dismissed, the AlertController is then uppermost, but its foreground is nowhere near as bright / white as the AlertView's was, OR as it was in the Test app example, image 2.

As previously stated, the behaviour is the same even if I eliminate the AlertView step (which is just for contrast).

One further observation - often when I observe the behaviour, just momentarily when an affected AlertController first appears, as in for maybe 1/4 or 1/8 of a second, its foreground is the correct whiteness/brightness, but then an instant later it becomes dimmed, per the stated problem.

One other observation - some AlertControllers in the real app display properly with the correct level of brightness, but many others do not.

One last observation - I have user the 'View UI Hierarchy' in Xcode and there does not appear to be anything 'on top of' the affected AlertController.

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}


override func viewDidAppear(_ animated: Bool) {


    UIAlertView(title: "My AlertView Title", message: "My AlertView Message", delegate: self, cancelButtonTitle: "OK").show()

    let alertTitle = "My AlertController Title"
    let msg = "\r\r\r\r\r\r\r\rMy AlertController Msg"

    let alertController = UIAlertController(title: alertTitle, message: msg, preferredStyle: .alert)

    let cancelAction = UIAlertAction(title: "Don't show this again", style: .cancel) { (action:UIAlertAction!) in
        print("Dismiss button pressed")

    }
    alertController.addAction(cancelAction)

    let OKAction = UIAlertAction(title: "Action 1", style: .default) { (action:UIAlertAction!) in

        print("I'm action1!!")
    }

    alertController.addAction(OKAction)

    let OK2Action = UIAlertAction(title: "Action2", style: .default) { (action:UIAlertAction!) in

        print("I'm action2")
    }

    alertController.addAction(OK2Action)

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


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

Any help as to this puzzle gratefully received, thanks!

EDIT:

thanks to @Richard G for putting me on the right track - I still have the problem but with more clarity.

So, it you look at the two screenshots below side by side, they are both from the real app, both use the code shown above to generate the UIAlertController (I have now delete the UIAlertView line as it isn't needed), the only difference between the one on the left and one on the right is the one on the right has these 3 lines of code added:

let backView = alertController.view.subviews.last?.subviews.last 
backView?.layer.cornerRadius = 10.0 
backView?.backgroundColor = UIColor.whiteColor()  

side by side UIAlertControllers 1 transparent 1 not

According to Apple, "The UIAlert​Controller class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified." OK, so this is hacking into it, and it proves that somehow the alertController has acquired, prior to displaying it, a backgroundColor which was partly transparent.

By setting the backgroundColor to UIColor.whiteColor() the problem is fixed, but the solution should be unnecessary - how on earth did the problem arise in the first place?

How was the backgroundColor somehow set to have a transparency when Apple doesn't even expose that property to me to be able to set?

any answers gratefully received!

Upvotes: 2

Views: 1683

Answers (2)

Mahendra
Mahendra

Reputation: 8914

After seeing an image that you have uploaded I can mention some point.

  • The UIAlertController is little bit transparent if you notice.

  • Since UIAlertController is transparent so the background will affect on appearance of alert controller.

NOTE: In case 1 & 2 background is white and case 3 & 4 background is black.

So what you can do it make background as bright as in case 1 and case 2 (from image).

**EDIT : **

TO change background color of UIAlertController you can do like...

let subView = alertController.view.subviews.first!;
var alertContentView = subView.subviews.first!;
alertContentView.backgroundColor = UIColor.darkGray;
alertContentView.layer.cornerRadius = 5;

Upvotes: 1

ron27
ron27

Reputation: 181

UIAlertView and UIAlertController has same background color and alpha value. But in your case AlertView is displayed above AlertController, ie why it feels like UIAlertView is Whitier than UIAlertController.

Upvotes: 0

Related Questions