如何加载具有透明背景颜色的 UIViewController?这样可以看到以前的 View Controller 。 我已尝试使用以下方法创建自定义 View ,但它不起作用。
- (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);
}
任何帮助都将不胜感激!
最佳答案
您可以通过添加 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];
然后在此 View 中,您可以添加所有 UI 元素。这将使它看起来像在旧 View 上放置了透明玻璃。
完成后,您只需从 super View 中删除即可。
[transprentView removeFromSuperview];
编辑:
或者你可以这样做like this answer .
希望对你有帮助, 朱利安
关于ios - 如何加载具有透明背景颜色的 UIViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31431790/