T.D
T.D

Reputation: 177

UIAlertView hidden behind View

I presented a view controller with the presentModalViewController:animated: method. Let's call this view myView. On myView, I have a button and when tapped, it is supposed to create a UIAlertView and show it.

However, when I tap this button, the alertView is created but doesn't appear on top of myView. Instead, it is hidden behind myView.

Note: To verify that the alertView is hidden behind myView, I added a second button on myView and when it is tapped, the myView view controller dismisses itself (i.e. [self dismissModalViewControllerAnimated:YES]). When myView is dismissed, the alertView appears.

Any idea why the alertView is hidden behind myView?

Upvotes: 3

Views: 1344

Answers (3)

leanne
leanne

Reputation: 8719

I was having this problem, also. It was intermittent, but I believe it related to displaying the alert on a background thread.

As of Xcode 9, Swift 4:

To test if function is running on main thread, use: Thread.current.isMainThread

print("On main thread: \(Thread.current.isMainThread)")

To display alert, or perform any operation on main thread, use OperationQueue.main:

OperationQueue.main.addOperation {
    // code to perform on main thread
}

Upvotes: 0

wgr
wgr

Reputation: 513

I have the same problem. In Controller1, I present Controller2, and in Controller2 I create an alertView. But the alertView is put behind the Controller2's view.

I found that if I create the alertView not from any viewController(by using a public function), the alertView will appear at the front of the screen.

+ (void)alertShow:(NSString *)alertMsg {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertMsg
                                                message:nil delegate:self
                                      cancelButtonTitle:nil
                                      otherButtonTitles:nil];

[alert show];

[self performSelector:@selector(dismissAlertView:) withObject:alert afterDelay:2.5];

}

Hope it will solve your problem.

Upvotes: 0

Zeeshan
Zeeshan

Reputation: 4244

I think after you show UIAlertView you are adding a subview on UIWindow. And it's above UIAlertView in UIWindow layer.

Make sure you don't add anything on UIWindow as it is not a good practice.

If you still wan t to carry out adding subview just send that subviewToBack:

Best of luck

Upvotes: 2

Related Questions