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

标签 ios objective-c

如何加载具有透明背景颜色的 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/

相关文章:

ios - 即使重复,如何获取特定子字符串的范围

javascript - 使用 Objective-C 将 JSON 解析为 C 风格的字符数组

objective-c - Objective C boolean 值

iphone - UITableView 部分问题

objective-c - 在 Objective-C 中将 float 转换为 int

objective-c - ALAssetRepresentation fullResolutionImage 永远不会返回

ios - 如何在特定行上查找和替换 UILabel 的值?

ios - 运行代码时出错?

objective-c - 如何禁用 Block 在 Xcode 10 中隐式保留 'self' 警告

iphone - 在运行时创建新的对象模板 iPhone