UIAlertView隐藏在视图后面

3
我使用presentModalViewController:animated:方法展示了一个视图控制器,让我们称之为myView。在myView上,我有一个按钮,当点击时,它应该创建一个UIAlertView并将其显示出来。
但是,当我点击此按钮时,alertView被创建但不会出现在myView的顶部,而是被隐藏在myView后面。
注意:为了验证alertView被隐藏在myView后面,我在myView上添加了第二个按钮。当点击该按钮时,myView视图控制器将自己解除(即[self dismissModalViewControllerAnimated:YES])。当myView被解除时,alertView就会出现。
任何想法为什么alertView被隐藏在myView后面?

你需要发布一些代码。 - Nick
3个回答

2
我认为在显示UIAlertView后,您会在UIWindow上添加一个子视图,并且它在UIWindow层中位于UIAlertView之上。
请确保不要在UIWindow上添加任何内容,因为这不是一个好的做法。
如果您仍然想要添加子视图,请使用sendSubviewToBack:方法。
祝你好运。

0

我之前也遇到了这个问题。虽然是间歇性的,但我相信它与在后台线程上显示提醒有关。

从Xcode 9,Swift 4开始:

要测试函数是否在主线程上运行,请使用:Thread.current.isMainThread

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

要显示警报或在主线程上执行任何操作,请使用OperationQueue.main

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

0

我有同样的问题。在Controller1中,我呈现了Controller2,在Controller2中我创建了一个alertView。但是alertView被放置在Controller2的视图后面。

我发现如果我不从任何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];

}

希望能解决您的问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接