2017-02-23 2 views
1

keyWindowにUIViewを追加すると、alertControllerはviewによってカバーされます。ここにコードがあります。UIAlertControllerはウィンドウのビューでカバーされています

- (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

解決方法をご存知ですか?

答えて

0

新しいUIViewをキーウィンドウではなくビューコントローラに追加します。ちょうどwindowにビューを追加することもwindowに添加し、その上に配置されていないすべてのビューをカバーする

[self.view addSubview:viewhaha]; 
1

にライン4

[currentWindow addSubview:viewhaha]; 

を変更します。ウィンドウレイヤーは、何かを追加できる最上位レイヤーです。 UIViewUIAlertViewControllerの下に配置する場合は、UIViewUIViewController.viewのサブビューに追加して、UIAlertControllerを表示する必要があります。

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; 

}私は@Brandon Aの答えウィンドウに同意

1

は、最上位層であり、それは、すべての背景は、私はスクリーンショット添付した

を見てカバーすることあなたを理解するのに役立ちます

enter image description here

enter image description here

関連する問題