15

自分でカスタム alertView (iOS7 以降用) を作成しようとしていますが、alertView のプレゼンテーションに苦労しています。

黒い背景 (アルファが 0.25f に設定) の UIViewController と、サブビューとしての alertView があります。

alertView を表示したい場合は、viewController をモーダルに表示します。

-(void) show 
{
    UIWindow* window = [[UIApplication sharedApplication] keyWindow];
    self.modalTransitionStyle = UIModalPresentationCustom;
    self.transitioningDelegate = self;
    [window.rootViewController presentViewController:self animated:YES completion:nil];
}

そして、ここに私のアニメーターオブジェクトがあります:

-(NSTimeInterval) transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    NSLog(@"%s",__PRETTY_FUNCTION__);
    return 2;
}

-(void) animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    NSLog(@"%s",__PRETTY_FUNCTION__);

    UIView* toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    toView.alpha = 0;

    UIView* container = [transitionContext containerView];
    [container addSubview:toView];

    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        toView.alpha = 0.5;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

問題は、モーダル VC がバックグラウンドで表示中の VC と一緒にフェードアウトすることですが、アニメーションが終了すると、表示中の VC がバックグラウンドから削除されます。

代わりに呼び出す[transitionContext completeTransition:YES];と、プレゼンテーション VC はバックグラウンドにありますが、モーダル VC はアニメーションの最後に削除されるため、'NO' を送信するとコンテキストがプレゼンテーションをキャンセルすると思います。

スナップショットを作成してモーダル VC のビューの背景として設定することなく、提示する VC をバックグラウンドで維持する方法はありますか?

4

6 に答える 6

42

このソリューションを試してみましたが、iOS 7 と 8 の両方で動作します。

if ([[UIDevice currentDevice].systemVersion integerValue] >= 8)
{
    //For iOS 8
    presentingViewController.providesPresentationContextTransitionStyle = YES;
    presentingViewController.definesPresentationContext = YES;
    presentedViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}
else
{
    //For iOS 7
    presentingViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
}

注: 「 presentingViewController」と「 presentedViewController 」の違いに注意してください。

于 2015-01-14T16:47:51.190 に答える
5

iOS8以降

iOS8 以降の場合、以下のコード スニペットを使用できます

SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;           
[self presentViewController:secondViewController animated:YES completion:nil];    
于 2017-01-11T07:35:18.173 に答える
2

私のケースはあなたのケースとは異なるかもしれませんが、情報は会話に役立つかもしれません.

ストーリーボードで、セグエのプレゼンテーションを「全画面表示」に変更すると、うまくいきました。

于 2015-09-22T10:25:17.597 に答える
1

ViewController は、提示またはプッシュするときに透過的であるべきではありません。サブビューとして追加してみてください。また、トランジション効果の場合は、サブビューとして追加した直後にそのフレームを変更します。フレームを可視ビューの外側のどこかに作成し、アニメーション化してフレームを可視ビューに変更します。

お役に立てれば。

于 2014-08-12T11:16:34.173 に答える
1

参考までに、最終的にカスタム alertView を「ポップアップ部分」の UIView のサブクラスにしました。それを表示するには、alertView を keyWindow のサブビューとして追加し、制約を中央に配置して、その背後に透明な黒の背景ビューを配置します。

コントローラーではないので、自分でUIの回転を管理する必要があります(iOS 7のみ、iOS 8のUIでうまく回転します)。

于 2014-08-28T07:09:39.670 に答える