このコードは、iOS6 および iOS7 の iPhone で正常に動作します。
presentedVC.view.backgroundColor = YOUR_COLOR; // can be with 'alpha'
presentingVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentingVC presentViewController:presentedVC animated:YES completion:NULL];
この場合、スライドオン アニメーションが見逃されます。アニメーションを保持するには、次の「エレガントではない」拡張機能を引き続き使用できます。
[presentingVC presentViewController:presentedVC animated:YES completion:^{
[presentedVC dismissViewControllerAnimated:NO completion:^{
presentingVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentingVC presentViewController:presentedVC animated:NO completion:NULL];
}];
}];
presentingV が UINavigationController または UITabbarController 内にある場合は、そのコントローラーを presentingVC として操作する必要があります。
さらに、iOS7 では、UIViewControllerTransitioningDelegate
プロトコルを適用するカスタム遷移アニメーションを実装できます。もちろん、この場合は透明な背景を得ることができます
@interface ModalViewController : UIViewController <UIViewControllerTransitioningDelegate>
まず、プレゼンテーションの前に設定する必要がありますmodalPresentationStyle
modalViewController.modalPresentationStyle = UIModalPresentationCustom;
次に、2 つのプロトコル メソッドを実装する必要があります。
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
CustomAnimatedTransitioning *transitioning = [CustomAnimatedTransitioning new];
transitioning.presenting = YES;
return transitioning;
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
CustomAnimatedTransitioning * transitioning = [CustomAnimatedTransitioning new];
transitioning.presenting = NO;
return transitioning;
}
CustomAnimatedTransitioning
最後に、クラスでカスタム遷移を定義します
@interface CustomAnimatedTransitioning : NSObject <UIViewControllerAnimatedTransitioning>
@property (nonatomic) BOOL presenting;
@end
@implementation CurrentContextTransitionAnimator
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return 0.25;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
if (self.presenting) {
// custom presenting animation
}
else {
// custom dismissing animation
}
}