如何创建一个半透明模式的UIViewController?(How can I create a t

2019-08-17 02:13发布

我想创建可以显示为比任何其他视图控制器模态的视图控制器可重复使用的UIViewController子类。 其中的第一件事情本可重复使用的VC需要做的是弹出一个UIActionSheet。 为了做到这一点,我在VC显示从动作片创建一个默认(空白)视图。

然而,这很糟糕,因为,当VC模式弹出,父VC是隐藏的。 因此它看起来像动作片是浮在空白背景。 它会更好,如果动作片可能出现流行在原始(父)VC。

有没有一种方法来实现这一目标? 它是安全的根本抢父VC的看法,并从动画UIActionSheet?

Answer 1:

你的模式视图动画中后,将被重新调整到在​​尺寸上它的父视图等。 你可以做的是你的viewDidAppear:,采取parentController的视图的图片中,然后插入包含父的图片在你自己的视图的子视图列表后面一个UIImageView:

#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];
}


Answer 2:

你可以简单地通过在父视图插入视图显示它在你的父视图控制器。

事情是这样的:

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];


Answer 3:

是的。 将其添加到当前视图控制器的视图(或作为窗口的子视图)和动画它屏幕上的像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];
}


文章来源: How can I create a translucent modal UIViewController?