ios 使 UIView 透明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15425149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:44:41  来源:igfitidea点击:

Making UIView transparent

iosuiviewscreentransparent

提问by peter1234

I want to add a UIView to my UIViewController but I want it to be transparent until I decide to create an Oval inside the UIView.

我想向我的 UIViewController 添加一个 UIView,但我希望它是透明的,直到我决定在 UIView 中创建一个椭圆。

Until I create the oval, I can set my view's alpha to 0 but when I want to add the oval, the background color is still black.

在创建椭圆之前,我可以将视图的 alpha 设置为 0,但是当我想添加椭圆时,背景颜色仍然是黑色。

here's what it looks like with a bunch of ovals

这是一堆椭圆的样子

image

图片

In my UIView:

在我的 UIView 中:

- (void)drawRect:(CGRect)rect
{
    [self pushContext];
    UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
    [[UIColor redColor] setFill];
    [oval fill];
    self.opaque = NO;
    [self setBackgroundColor:[UIColor clearColor]];

    [oval addClip];
    [self setBackgroundColor:[UIColor clearColor]];

    [self popContext];
}

回答by Dharmbir Singh

Please try to use this one

请尝试使用这个

view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
view.opaque = NO;

回答by Guo Luchuan

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        self.opaque = NO;

    }
    return self;
}



- (void)drawRect:(CGRect)rect
{
    [[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0] setFill];
    UIRectFill(rect);
    [self pushContext];
    UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
    [[UIColor redColor] setFill];
    [oval fill];

    [oval addClip];

    [self popContext];
}

回答by Natasha

In your ViewController, you could say that opaque is NO. Let's say, your view is named myCustomView.

在您的 ViewController 中,您可以说 opaque 为 NO。假设您的视图名为 myCustomView。

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myCustomView.opaque = NO;   
}

Or you could just go to the storyboard and uncheck the opaque checkbox like- enter image description here

或者您可以直接转到故事板并取消选中不透明的复选框,例如- 在此处输入图片说明

回答by Matt

In Swift 3.x: (where layeris the view)

在 Swift 3.x 中:(视图

layer.backgroundColor = UIColor.clear.cgColor
layer.isOpaque = false

回答by Northern Captain

I did semi-transparent background for my UIViewController the same way as it's recommended here but it didn't work until I set modalPresentationStyleto .custom

我为我的 UIViewController 做了半透明背景与这里推荐的相同,但直到我将modalPresentationStyle设置为.custom起作用

my Swift 4code:

我的Swift 4代码:

modalPresentationStyle = .custom
view.isOpaque = false
view.backgroundColor = UIColor.black.withAlphaComponent(0.2)

I open my controller from another one using present(controller, anim, completion) method.

我使用present(控制器,动画,完成)方法从另一个控制器打开我的控制器。