[英]How to load UIViewController with transparent Background color?
如何加载具有透明背景色的UIViewController? 这样就可以看到以前的视图控制器。 我尝试使用以下方法创建自定义视图,但是它不起作用。
- (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);
}
任何帮助将不胜感激!
您可以通过添加子视图来实现。
//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];
然后,可以在此视图中添加所有UI元素。 这将使它看起来像是将透明玻璃放在旧视图上。
完成后,您只需从超级视图中删除即可。
[transprentView removeFromSuperview];
编辑:
或者您也可以像这样回答 。
希望有帮助,朱利安
使您的视图控制器的视图透明viewController.view.backgroundColor = [UIColor clearColor];
但是,在任何情况下都无法帮助您查看其背后的视图控制器内容。 例如,如果您的控制器位于导航控制器中,则不会。 在这种情况下-在将新控制器推到顶部之前,请使用方法制作快照视图,并将其用作顶部控制器所有内容下的一层。
喜欢
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.