iOS8にしてからUIModalPresentationCurrentContextで背景を透過できなくなっていました。
これまで
背景透過ってかっこいいですよね!
たまに無性に使いたくなるときとかあると思います。
そんなとき、以下のような感じで実装しているのではないでしょうか?
これだとiOS8になったときに一切背景を透過させず、真っ暗になってしましました。。
AnyViewController
// vcというViewControllerを透過して,今のViewを透かして見せたいとき
vc.view.frame = CGRectMake(0.0, 0.0, screen.size.width, screen.size.height);
vc.view.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7];
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
iOS8で背景を透過するには
iOS8でも背景を透過させるためにはUIModalPresentationOverCurrentContextを使います。
以下のように1行だけ変えれば透過させることができます。
AnyViewController
// vcというViewControllerを透過して,今のViewを透かして見せたいとき
vc.view.frame = CGRectMake(0.0, 0.0, screen.size.width, screen.size.height);
vc.view.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7];
// ここを変更!
vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
注意
ちなみに、このように変更するとiOS8以前のもので背景が透過できなくなってしまいました。
みんなiOS8にしろやーってのもありですが、ユーザーのためにどちらのOSでも透過できるようにしてあげるのが良いと思います。
OSを調べるには[[UIDevice currentDevice].systemVersion floatValue]を使います。
これを使うと、背景透過のiOS8対応は以下のようになります。
AnyViewController
// vcというViewControllerを透過して,今のViewを透かして見せたいとき
vc.view.frame = CGRectMake(0.0, 0.0, screen.size.width, screen.size.height);
vc.view.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7];
// iOS8とそれ以外で分岐
if([[UIDevice currentDevice].systemVersion floatValue] < 8.0){
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
} else {
vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}
[self presentViewController:vc animated:NO completion:nil];
iOSのメジャーアップデートは大きな変更はアナウンスされるものの、このような小さな変更は何も言われないので困りますね。他にも何か見つけたらアップしたいと思います。