2009-09-30 8 views
7

私は他のビューコントローラよりもモーダルビューコントローラとして表示できる再利用可能なUIViewControllerサブクラスを作成したいと思います。この再利用可能なVCが最初に必要とすることの1つは、UIActionSheetをポップアップすることです。これを行うために、自分のVCにデフォルトの(空白の)ビューを作成して、アクションシートを表示します。半透明のモーダルUIViewControllerを作成するにはどうすればよいですか?

しかし、モーダルのVCがポップアップすると、親のVCが非表示になっているため、これは悪いようです。したがって、アクションシートが空白の背景に浮いているように見えます。アクションシートが元の(親)vcにポップするように見える場合は、より良いでしょう。

これを達成する方法はありますか?単に親ビューのビューを取得してUIActionSheetをアニメーション化するのは安全ですか?

+0

を同じ問題を抱えて:あなたはどうすることができますことは、サブビューの独自のビューのリストの後ろに親の絵を含むUIImageViewを挿入し、その後、parentControllerのビューの写真を撮る:,あなたのviewDidAppearの内側にあります。 。 任意のソリューション?? – Frade

答えて

0

親ビューにビューを挿入するだけで親ビューコントローラに表示することができます。このような

何か:

PseudoModalVC *vc = ...//initialization 
vc.view.backgroundColor = [UIColor clearColor]; // like in previous comment, although you can do this in Interface Builder 
vc.view.center = CGPointMake(160, -vc.view.bounds.size.height/2); 
[parentVC.view addSubView:vc.view]; 

// animation for pop up from screen bottom 
[UIView beginAnimation:nil context:nil]; 
vc.view.center = CGPointMake(160, vc.view.bounds.size.height/2); 
[UIView commitAnimation]; 
+1

これを行うと、表示されたビューコントローラの "viewWillAppear"のようなメソッドへの適切な呼び出しもすべて行う必要があります。 UINavigationControllerとモーダルシステムがあなたのためにこれを普通に処理します。 – DougW

-1

うん。これを現在のView Controllerのビュー(またはウィンドウのサブビュー)に追加し、Valerii氏のように画面上にアニメーション表示します。

はこれを行う、アニメーションでそれを削除するには(私はモーダルビューは320×460であると仮定することだし、それは画面の外に下にスライドします):

- (void)dismissModal 
{ 
    // animate off-screen 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
    [UIView setAnimationDuration:0.50]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 

    self.view.frame = CGRectMake(0, 480, 320, 460); 

    [UIView commitAnimations]; 
} 

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{ 
    // don't remove until animation is complete. otherwise, the view will simply disappear 
    [self.view removeFromSuperview]; 
} 
14

あなたのモーダルビューがでアニメーション化した後、それを親ビューと同じサイズにリサイズされます。

#pragma mark - 
#pragma mark Sneaky Background Image 
- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 

    // grab an image of our parent view 
    UIView *parentView = self.parentViewController.view; 

    // For iOS 5 you need to use presentingViewController: 
    // UIView *parentView = self.presentingViewController.view; 

    UIGraphicsBeginImageContext(parentView.bounds.size); 
    [parentView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *parentViewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    // insert an image view with a picture of the parent view at the back of our view's subview stack... 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; 
    imageView.image = parentViewImage; 
    [self.view insertSubview:imageView atIndex:0]; 
    [imageView release]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 

    // remove our image view containing a picture of the parent view at the back of our view's subview stack... 
    [[self.view.subviews objectAtIndex:0] removeFromSuperview]; 
} 
+1

iOS 5では、 'parentViewController'の代わりに' presentingViewController'を使う必要があります。あなたの答えを更新しました。 –

+0

これは主にうまくいきますが、試してみると、親ビュー画像の上部に20 pxのギャップがあり、ステータスバーによって(私は推測しています)それを修正するための提案はありますか? – Mac

+0

更新:画像のコンテキストを20ピクセル以上上に翻訳して修正を管理しましたが、他の誰も持っていないように見える理由についてはまだ興味がありますか? – Mac

関連する問題