ぎじゅつめもブログ

主にアプリ開発の技術メモを残していきます。

【Objective-C】背景が透明なUIViewControllerを呼び出す(iOS8)

背景が透明なUIViewControllerをiOS8で呼び出す方法です。今までと少しやり方が異なっています。
(確認: xcode6.1 iOS8.1)

    // 適当にViewControllerを作って...
    UIViewController* bViewController = [[UIViewController alloc] init];
    CGRect screen = [[UIScreen mainScreen] bounds];
    bViewController.view.frame = CGRectMake(0.0, 0.0, screen.size.width, screen.size.height);
    bViewController.view.backgroundColor = [UIColor colorWithRed:0.0 green:128.0 blue:128.0 alpha:0.3];
    
    // 透明になるよう設定...
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        self.providesPresentationContextTransitionStyle = YES;
        self.definesPresentationContext = YES;
        [bViewController setModalPresentationStyle:UIModalPresentationOverCurrentContext]; // selfは呼び出し元のUIViewController
    } else {
        self.modalPresentationStyle = UIModalPresentationCurrentContext;
    }
    [self presentViewController:bViewController animated:YES completion:nil];

以上です。