Skip to content

UIAlertController is hidden behind the shadow. #110

Closed
@Salmancs43

Description

@Salmancs43

Hi When i present the UIAlertController it is showing behind the overlay. Is there any suggestion for that? Thanks
simulator screen shot may 4 2017 4 51 54 pm

Activity

ephread

ephread commented on May 18, 2017

@ephread
Owner

Hello @Salmancs43, sadly, nothing can be done at the moment. But there's hope, next version will ship with proper support of window levels.

ephread

ephread commented on Jun 3, 2017

@ephread
Owner

I've always believed that UIAlertController views were presented in their own window. Turns out I was mistaken. You're going to need to present the UIAlertController in its own window, which will sit above Instructions' window. By default, Instructions uses a window with the following level: UIWindowLevelNormal + 1 (it's customizable, but not yet in the README). The custom window will need to have an higher value than that.

Here's a possible way to achieve what, from the top of my head (untested).

let window = UIWindow(frame: UIScreen.main.bounds)
let dummyViewController = UIViewController()

dummyViewController.view.backgroundColor = UIColor.clear
window.rootViewController = dummyViewController
window.windowLevel = UIWindowLevelNormal + 2

// Instructions doesn't use a key window, but the alert probably should.
window.makeKeyAndVisible()

dummyViewController.present(alertViewController, animated: true, completion: nil)

After the alert is dismissed, we'll probably need to make sure that the key window is again the main window, and not the one used by Instructions. It's most likely done automatically, but I haven't checked.

Salmancs43

Salmancs43 commented on Jun 5, 2017

@Salmancs43
Author
    let alertWindow = UIWindow(frame: UIScreen.main.bounds)
        alertWindow.rootViewController = UIViewController()
        alertWindow.windowLevel = UIWindowLevelAlert + 1;
        alertWindow.makeKeyAndVisible()
        
        let alert = UIAlertController(title:"Please Select an Option", message:nil , preferredStyle: .actionSheet)
        
        alert.addAction(UIAlertAction(title: "Camera", style: .default , handler:{ (UIAlertAction)in
            print("User click Camera button")
            
            self.openCamera()
        }))
        
        alert.addAction(UIAlertAction(title: "Photo Library", style: .default , handler:{ (UIAlertAction)in
            print("User click Photo button")
            self.openGallary()
            
        }))
        alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.cancel, handler:{ (UIAlertAction)in
            print("User click Dismiss button")
            
            
        }))
        alertWindow.rootViewController?.present(alert, animated: true, completion: {
            print("completion block")
        })

It Can be achieved using this code.

ephread

ephread commented on Jun 5, 2017

@ephread
Owner

Problem solved!

kennenfromchina

kennenfromchina commented on Dec 25, 2018

@kennenfromchina

I've always believed that UIAlertController views were presented in their own window. Turns out I was mistaken. You're going to need to present the UIAlertController in its own window, which will sit above Instructions' window. By default, Instructions uses a window with the following level: UIWindowLevelNormal + 1 (it's customizable, but not yet in the README). The custom window will need to have an higher value than that.

Here's a possible way to achieve what, from the top of my head (untested).

let window = UIWindow(frame: UIScreen.main.bounds)
let dummyViewController = UIViewController()

dummyViewController.view.backgroundColor = UIColor.clear
window.rootViewController = dummyViewController
window.windowLevel = UIWindowLevelNormal + 2

// Instructions doesn't use a key window, but the alert probably should.
window.makeKeyAndVisible()

dummyViewController.present(alertViewController, animated: true, completion: nil)

After the alert is dismissed, we'll probably need to make sure that the key window is again the main window, and not the one used by Instructions. It's most likely done automatically, but I haven't checked.

perfect!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ephread@kennenfromchina@Salmancs43

        Issue actions

          UIAlertController is hidden behind the shadow. · Issue #110 · ephread/Instructions