窗口上的视图覆盖了UIAlertController

时间:2017-02-23 04:06:42

标签: ios objective-c uialertcontroller

当我向keyWindow添加UIView时,viewController覆盖了alertController。这是代码。

- (void)touch {
    UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
    UIView *viewhaha = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    viewhaha.backgroundColor = [UIColor redColor];
    [currentWindow addSubview:viewhaha];

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"123" message:@"123" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"123" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];

    [alertController addAction:action1];
    [self presentViewController:alertController animated:YES completion:^{
    }];
}    

这是图像。 Image

你知道怎么解决吗?

4 个答案:

答案 0 :(得分:1)

public class StackWithMin extends Stack<Integer> { private Stack<Integer> min; public StackWithMin() { min = new Stack<>(); } public void push(int num) { if (super.isEmpty()) { min.push(num); } else if (num <= min.peek()) { min.push(num); } super.push(num); } public int min() { return min.peek(); } public Integer pop() { if (super.peek() == min.peek()) { min.pop(); } return super.pop(); } } 添加视图将涵盖任何未添加到window并置于其上的视图。窗口图层是您可以添加内容的最上层。如果您希望将window放在UIView下方,则必须将UIAlertViewController添加到UIView UIViewController的子视图中正在展示.view

答案 1 :(得分:1)

我同意@Brandon A的答案窗口是最顶层,它将涵盖所有背景视图

我附上了有助于您理解的屏幕截图

enter image description here

enter image description here

答案 2 :(得分:0)

将新的UIView添加到视图控制器而不是关键窗口。只需改变第4行

[currentWindow addSubview:viewhaha];

[self.view addSubview:viewhaha];

答案 3 :(得分:0)

- (void)touch {    
UIViewController *currentTopVC = [self currentTopViewController];

UIView *viewhaha = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
viewhaha.backgroundColor = [UIColor redColor];
[currentTopVC.view addSubview:viewhaha];

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"123" message:@"123" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"123" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];

[alertController addAction:action1];
[currentTopVC presentViewController:alertController animated:YES completion:^{
}];

}

- (UIViewController *)currentTopViewController {
UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (topVC.presentedViewController)
{
    topVC = topVC.presentedViewController;
}
return topVC;

}