若要以半透明模態方式顯示自訂彈出視窗,可設定 modalPresentationStyle。在 iOS 8 以上,通常使用 UIModalPresentationOverCurrentContext,讓背景保持可見。
MKDialogController *controller = [[MKDialogController alloc] init];
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
controller.providesPresentationContextTransitionStyle = YES;
controller.definesPresentationContext = YES;
controller.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:controller animated:YES completion:nil];
}
若需要相容 iOS 7,可改用 rootViewController 的 modalPresentationStyle,在 present 前後切換 UIModalPresentationCurrentContext 與 UIModalPresentationFullScreen。實際效果會受系統版本與控制器階層影響,建議在真機與模擬器上測試。