简体   繁体   English

UIViewController透明背景

[英]UIViewController transparent background

I am creating a viewcontroller in this way: 我正在以这种方式创建一个viewcontroller:

 UIStoryboard *sb = [UIStoryboard storyboardWithName:@"PhotoViewControlleriPhone" bundle:nil];
                                UIViewController *vc = [sb instantiateInitialViewController];

                               vc.view.backgroundColor = [UIColor clearColor];
                               self.modalPresentationStyle = UIModalPresentationCurrentContext;

                               [self presentModalViewController:vc animated:NO];

                               vc.view.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y + 64, imageView.frame.size.width, 200.000000);

                               vc.view.layer.cornerRadius = 10; // this value vary as per your desire
                               vc.view.clipsToBounds = YES;

The viewcontroller is not full screen, so you can still see the previous one. viewcontroller不是全屏,因此您仍然可以看到前一个。 I want to be able to see it, but lock it. 我想能够看到它,但锁定它。 Just like when you use ios facebook sharing, you see the background, but it becomes darker and you can't interact with it. 就像当你使用ios facebook分享时,你会看到背景,但它会变得更暗,你无法与之交互。 How can I do this? 我怎样才能做到这一点?

I believe the problem is that you're displaying it using -presentModalViewController:animated: . 我相信问题是你使用-presentModalViewController:animated:来显示它。 Using that method carries with it some assumptions about the view controller you're hosting; 使用该方法带有关于您正在托管的视图控制器的一些假设; one of the assumptions it makes (on iPhone-type devices, at least) is that the view controller takes up the entire screen and is opaque. 它所做的一个假设(至少在iPhone型设备上)是视图控制器占据整个屏幕并且是不透明的。

To get around this, try adding the modal view controller's view to the current view controller's view manually. 要解决此问题,请尝试手动将模态视图控制器的视图添加到当前视图控制器的视图中。 You'll need to set the view controller hierarchy up to match the view hierarchy, so your code would look like this: 您需要将视图控制器层次结构设置为与视图层次结构匹配,因此您的代码将如下所示:

[self addChildViewController:vc];
[self.view addSubview:vc.view];

You'll need to adjust the incoming view's frame to position it within its new superview, but that should allow you more freedom. 您需要调整传入视图的帧以将其定位在新的超视图中,但这样可以让您更自由。

My workaround is to take a screenshot with code , pass the UIImage as a parameter to the new UIViewController, and display it as a background image. 我的解决方法是使用代码截取屏幕截图 ,将UIImage作为参数传递给新的UIViewController,并将其显示为背景图像。 In that way it appears transparent, and the you don't have to disable the underlying controls that might be reachable/accessible. 通过这种方式,它看起来是透明的,您不必禁用可访问/可访问的基础控件。

iOS8+ iOS8上+

You can use below code snippet for iOS8+, its worked for me 您可以在iOS8 +下使用以下代码段,它适用于我

SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;           
[self presentViewController:secondViewController animated:YES completion:nil];    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM