日々精進

新しく学んだことを書き留めていきます

iOS7,8でモーダルビューの背景色を半透明にする

iOS7と8でやり方が違うので、以下のように分岐させる。 IS_GREATER_THAN_OR_EQUAL_TO_IOS_8は独自に定義したマクロ。

    // モーダルビューの背景を半透明にし、fadein, fadeoutさせるための設定
    if (IS_GREATER_THAN_OR_EQUAL_TO_IOS_8) {
        // これが無いとモーダルビューを閉じた時にfadeoutしない
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        self.providesPresentationContextTransitionStyle = YES;
        self.definesPresentationContext = YES;
    } else {
        self.modalPresentationStyle = UIModalPresentationCurrentContext;
    }
    
    UIViewController *modalViewController = [[UIViewController alloc] init];
    modalViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    modalViewController.view.frame = self.view.bounds;
    modalViewController.view.backgroundColor = <半透明の色>;
    if (IS_GREATER_THAN_OR_EQUAL_TO_IOS_8) {
        modalViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    }

    CGRect beforeFrame = vc.view.frame;
    [self presentViewController:modalViewController animated:YES completion:nil];
    }

参考:

http://www.raywenderlich.com/forums/viewtopic.php?f=2&t=18661#p76609