简体   繁体   English

隐藏在View背后的UIAlertView

[英]UIAlertView hidden behind View

I presented a view controller with the presentModalViewController:animated: method. 我用presentModalViewController:animated:方法呈现了一个视图控制器。 Let's call this view myView. 我们将此视图称为myView。 On myView, I have a button and when tapped, it is supposed to create a UIAlertView and show it. 在myView上,我有一个按钮,当点击时,它应该创建一个UIAlertView并显示它。

However, when I tap this button, the alertView is created but doesn't appear on top of myView. 但是,当我点击此按钮时,会创建alertView,但不会显示在myView之上。 Instead, it is hidden behind myView. 相反,它隐藏在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 (ie [self dismissModalViewControllerAnimated:YES] ). 注意:为了验证alertView隐藏在myView后面,我在myView上添加了第二个按钮,当它被点击时,myView视图控制器自行解除(即[self dismissModalViewControllerAnimated:YES] )。 When myView is dismissed, the alertView appears. 当myView被解除时,会出现alertView。

Any idea why the alertView is hidden behind myView? 知道为什么alertView隐藏在myView后面吗?

I think after you show UIAlertView you are adding a subview on UIWindow. 我想在您展示UIAlertView后,您将在UIWindow上添加子视图。 And it's above UIAlertView in UIWindow layer. 它位于UIWindow层中的UIAlertView之上。

Make sure you don't add anything on UIWindow as it is not a good practice. 确保您不在UIWindow上添加任何内容,因为这不是一个好习惯。

If you still wan t to carry out adding subview just send that subviewToBack: 如果你还想继续添加子视图,只需发送subviewToBack:

Best of luck 祝你好运

I have the same problem. 我也有同样的问题。 In Controller1, I present Controller2, and in Controller2 I create an alertView. 在Controller1中,我呈现Controller2,在Controller2中我创建了一个alertView。 But the alertView is put behind the Controller2's view. 但是alertView被置于Controller2的视图之后。

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. 我发现如果我不是从任何viewController创建alertView(通过使用公共函数),alertView将出现在屏幕的前面。

+ (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. 希望它能解决你的问题。

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: 从Xcode 9开始,Swift 4:

To test if function is running on main thread, use: Thread.current.isMainThread 要测试函数是否在主线程上运行,请使用: 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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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