简体   繁体   中英

Background Color Randomly Changes of UIViewController presented Modally

So I have a UIViewController (AViewController) that has a UIButton that modally presents another UIViewController (BViewController). BViewController has a UIVisualEffectView that blurs AViewController. In storyboard, I have set the background color to 0% opacity of the following: the View of the BViewController, and the UIVisualEffectView and its View. So it should be completely transparent except and just have the blurring effect.

Most of the time this works as expected. However, about 1 out of 3 times (but randomly ie I can't find a pattern) the background of ViewController is dark grey instead of clear. And it is not transparent.

I have tried to programmatically ensure that the color is clear. If I set the backgrounds (of BViewController and the UIVisualEffectView and its View) to clearColor in viewDidLoad, nothing changes ie it works about every 3 out of 4 times. However, if I set the backgrounds in viewDidAppear then it turns the exact color of dark grey and not transparent at all EVERY TIME (instead of 1 out 3 times).

The code is straight forward and the UIViewControllers are set up in storyboard.

In AViewController

var aViewController = storyboard!.instantiateViewControllerWithIdentifier("AViewController") as! 

self.presentViewController(aViewController, animated: false, completion: nil)

In BViewController

self.dismissViewControllerAnimated(false, completion: nil)

**EDIT: SIMPLE TEST PROJECT IN XCODE TO REPLICATE ISSUE **

This is giving me dark grey background 9/10 times - making me realize that I have never presented a modal UIViewController without a segue before.

BViewController

class BViewController: UIViewController {
    @IBAction func exit(sender: AnyObject) {
        self.dismissViewControllerAnimated(false, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

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

}

AViewController

class AViewController: UIViewController {

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

    }

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


    @IBAction func present(sender: AnyObject) {
        var bViewController = storyboard!.instantiateViewControllerWithIdentifier("bViewController") as! BViewController

        self.presentViewController(bViewController, animated: false, completion: nil)

    }
}

With Segue in connection

To my knowledge it is happening because when ever you present a view controller with a segue it gets presented over the view controller that it is connected to and is also put in the current stack only ie the stack in which the ViewControllerA was.

Without segue in connection ie INITIATING a ViewController

But when you INITIATE a view controller ie ViewControllerB it gets loaded in a new stack and hence it doesn't find any view controller below it and because of that it show a dark grey or black color when making the Main View of ViewControllerB's color to CLEAR Color.

//I hope i was able to convey my self and was helpful to you. Happy Coding :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM