如何创建一个半透明的模式UIViewController? [英] How can I create a translucent modal UIViewController?

查看:170
本文介绍了如何创建一个半透明的模式UIViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to create a reusable UIViewController subclass that can be shown as a modal view controller over any other view controller. One of the first things this reusable VC needs to do is pop up a UIActionSheet. In order to do this, I create a default (blank) view in my VC to show the action sheet from.

但是,这看起来不好,因为当模态vc弹出,父vc被隐藏。因此,它看起来像操作表悬浮在空白背景。如果操作表看起来像是弹出原来的(父)vc,那会更好。

However, this looks bad because, when the modal vc pops up, the parent vc is hidden. Thus it looks like the action sheet is floating over a blank background. It'd be better if the action sheet could appear to pop over the original (parent) vc.

有办法实现吗?是简单地抓住父vc的视图和动画UIActionSheet安全吗?

Is there a way to achieve this? Is it safe to simply grab the parent vc's view and animate the UIActionSheet from that?

推荐答案

在模态视图动画之后,它的大小被调整为等于其父视图的大小。你可以做的是在你的viewDidAppear :,拍摄parentController的视图的图片,然后插入一个UIImageView包含父视图的自己的视图的子视图列表后面的图片:

After your modal view animates in, it is resized to be equal in size to its parent view. What you can do is inside your viewDidAppear:, take a picture of the parentController's view, then insert a UIImageView containing the picture of the parent at the back of your own view's list of subviews:

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

这篇关于如何创建一个半透明的模式UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆