ぎじゅつめもブログ

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

【Objective-C】背景が透明のViewControllerを呼び出す

UIViewController(B)のViewを透明にして、UIViewController(A)が見えるようにしたいときのメモです。
(xcode5, iOS7)

iOS8はこちら(http://tsuyushiga.hatenablog.jp/entry/2014/12/10/211447)

  • イメージ

f:id:tsuyushiga:20140131220118p:plain
modalPresentationStyleをUIModalPresentationCurrentContextに指定します。

// 適当にUIViewControllerを作って...
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:0.0 blue:0.0 alpha:0.8];

// selfがUIViewController(A)だと思ってください...
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:bController animated:YES completion:nil];


しかしこのままだとViewが出現するときに何もアニメーションがありません。
なのでUIViewController(B)のViewWillAppearに出現アニメーションを書きます。

CGFloat h = SCREEN_BOUNDS.size.height;
CGFloat w = SCREEN_BOUNDS.size.width;
[UIView beginAnimations:nil context:nil];
[self.navigationController.view setFrame:CGRectMake(0.0, h, w, h)];
[UIView setAnimationDuration:0.4f];
[self.navigationController.view setFrame:CGRectMake(0.0, 0.0, w, h)];
[UIView commitAnimations];


*参考サイト
http://d.hatena.ne.jp/uokumura/touch/20130426/1366940299


以上です。