UIViewController の上に clearColor UIViewController を表示する 質問する

UIViewController の上に clearColor UIViewController を表示する 質問する

UIViewController別のビューの上にサブビュー/モーダルとしてビューを配置していますUIViewController。サブビュー/モーダルは透明で、サブビューに追加されたコンポーネントはすべて表示される必要があります。問題は、サブビューが clearColor ではなく黒い背景を表示することです。UIView黒い背景ではなく clearColor にしようとしています。何が間違っているのか知っている人はいませんか? 何かアドバイスがあればお願いします。

最初のViewController.m

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];

[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:NO];  

セカンドビューコントローラ.m

- (void)viewDidLoad 
{
     [super viewDidLoad];
     self.view.opaque = YES;
     self.view.backgroundColor = [UIColor clearColor];
}

解決済み: 問題を修正しました。iPhone と iPad の両方でうまく動作しています。黒い背景のない、clearColor/transparent だけのモーダル ビュー コントローラーです。変更する必要があるのは、UIModalPresentationFullScreenを に置き換えることだけですUIModalPresentationCurrentContext。とても簡単ですね。

最初のViewController.m

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];

知らせ:modalPresentationStyleのプロパティを使用している場合navigationController:

最初のViewController.m

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];

注意: 残念なことに、上記の解決策は iOS 7 では機能しません。良いニュースは、iOS 7 の問題を修正したことです。誰かに助けを求めたところ、次のように返答がありました。

ビュー コントローラをモーダルで表示すると、iOS は、表示中は、その下にあるビュー コントローラをビュー階層から削除します。モーダルで表示されるビュー コントローラのビューは透明ですが、その下には黒いアプリ ウィンドウ以外には何も表示されません。iOS 7 では、新しいモーダル表示スタイルが導入されました。UIModalPresentationCustomこれにより、iOS は、表示されたビュー コントローラの下にあるビューを削除しません。ただし、このモーダル表示スタイルを使用するには、プレゼンテーションと終了アニメーションを処理する独自のトランジション デリゲートを提供する必要があります。これは、WWDC 2013 の「ビュー コントローラを使用したカスタム トランジション」の講演で説明されています。https://developer.apple.com/wwdc/videos/?id=218また、独自の遷移デリゲートを実装する方法についても説明します。

iOS7 での上記の問題に対する私の解決策を次に示します。https://github.com/hightech/iOS-7-カスタム-ModalViewController-Transitions

ベストアンサー1

iOS8以降

iOS8 以降では、新しい modalPresentationStyle UIModalPresentationOverCurrentContext を使用して、透明な背景を持つビュー コントローラーを表示できるようになりました。

MyModalViewController *modalViewController = [[MyModalViewController alloc] init];
modalViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;           
[self presentViewController:modalViewController animated:YES completion:nil];    

おすすめ記事