Iphone 如何创建半透明模态UIViewController?
我想创建一个可重用的UIViewController子类,它可以作为模式视图控制器显示在任何其他视图控制器上。这个可重用VC需要做的第一件事就是弹出一个UIActionSheet。为了做到这一点,我在VC中创建了一个默认(空白)视图,以显示来自的操作表 然而,这看起来很糟糕,因为当模态vc弹出时,父vc被隐藏。因此,动作表似乎漂浮在空白背景上。如果行动表能够在原始(父)vc上弹出,那就更好了Iphone 如何创建半透明模态UIViewController?,iphone,cocoa-touch,Iphone,Cocoa Touch,我想创建一个可重用的UIViewController子类,它可以作为模式视图控制器显示在任何其他视图控制器上。这个可重用VC需要做的第一件事就是弹出一个UIActionSheet。为了做到这一点,我在VC中创建了一个默认(空白)视图,以显示来自的操作表 然而,这看起来很糟糕,因为当模态vc弹出时,父vc被隐藏。因此,动作表似乎漂浮在空白背景上。如果行动表能够在原始(父)vc上弹出,那就更好了 有没有办法做到这一点?简单地抓住父vc的视图并从中设置UIActionSheet的动画是否安全 只需在父
有没有办法做到这一点?简单地抓住父vc的视图并从中设置UIActionSheet的动画是否安全 只需在父视图中插入视图,即可在父视图控制器上显示它 大概是这样的:
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];
是的。将其添加到当前视图控制器的视图(或作为窗口的子视图),并像Valerii所说的那样在屏幕上设置动画 要使用动画删除它,请执行以下操作(我假设模式视图为320 x 460,它将从屏幕上滑下):
模式视图在中设置动画后,其大小将调整为与其父视图相同。您可以在ViewDidDisplay:中,拍摄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];
}
如果执行此操作,还需要在显示的视图控制器中对“ViewWillExample”等方法进行所有适当的调用。UINavigationController和模式系统通常会为您处理此问题。对于iOS 5,您需要使用
presentingViewController
而不是parentViewController
。我更新了你的答案,将其包括在内。这通常效果很好,但当我尝试它时,我在父视图图像的顶部得到一个20像素的间隙,这是由状态栏造成的(我猜)。有什么建议可以纠正吗?更新:我已经通过向上翻译20个像素的图像上下文来解决问题,但我仍然很好奇为什么我必须这样做,因为似乎没有其他人这样做过?听起来你使用的屏幕空间与其他屏幕空间不同。例如,您可能没有为您的应用程序在顶部显示状态栏,而其他应用程序可能会显示状态栏。@Mac您可能需要将模式视图控制器的WantFullScreenLayout
属性设置为“是”。存在相同的问题。。有什么解决办法吗??
#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];
}