简体   繁体   English

如何加载具有透明背景色的UIViewController?

[英]How to load UIViewController with transparent Background color?

How to load UIViewController with transparent Background color? 如何加载具有透明背景色的UIViewController? So that previous view controller can be visible. 这样就可以看到以前的视图控制器。 I have tried creating custom view with below method but it does not work. 我尝试使用以下方法创建自定义视图,但是它不起作用。

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    {
        CGColorRef colorRef=[UIColor clearColor].CGColor;
        CGContextSetFillColorWithColor(ctx, colorRef);
//        CGColorRelease(colorRef);
        CGContextFillRect(ctx, rect);
    }
    CGContextRestoreGState(ctx);
}

Any help will be appreciable! 任何帮助将不胜感激!

You can do that by adding a subview. 您可以通过添加子视图来实现。

//Get your screen size
CGRect screenBounds = [[UIScreen mainScreen] bounds];
//Create the transparent view of the same size
UIView* transparentView = [[UIView alloc] initWithFrame:screenBounds];
// change the background color to black and the opacity to 0.5 as this will make it look transparent
transparentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
// add this new view to your main view
[self.view addSubview:transparentView];

Then in this view you can add all of your UI Elements. 然后,可以在此视图中添加所有UI元素。 This will make it look like a transparent glass is put over the old view. 这将使它看起来像是将透明玻璃放在旧视图上。

When you're finished, you just remove from super view. 完成后,您只需从超级视图中删除即可。

[transprentView removeFromSuperview];

EDIT: 编辑:

Or you could do it like this answer . 或者您也可以像这样回答

Hope that helps, Julian 希望有帮助,朱利安

To make your view controller's view transparent viewController.view.backgroundColor = [UIColor clearColor]; 使您的视图控制器的视图透明viewController.view.backgroundColor = [UIColor clearColor];

But, it won't help you to see content of view controller behind it in all situations. 但是,在任何情况下都无法帮助您查看其背后的视图控制器内容。 For example, if your controllers are in navigation controller - it wouldn't. 例如,如果您的控制器位于导航控制器中,则不会。 In that case - before pushing new controller on top, make snapshot view with method and use it as an layer under all content of top controller. 在这种情况下-在将新控制器推到顶部之前,请使用方法制作快照视图,并将其用作顶部控制器所有内容下的一层。

Like 喜欢

 UIView *backgroundView = [navVC.topViewController.view snapshotViewAfterScreenUpdates:NO];

 [nextVC.view insertSubview:backgroundView atIndex:0];
 [navVC psuhViewController:nextVC animated:YES];

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

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