使UIView透明化

问题描述 投票:13回答:5

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

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

这是一堆椭圆形的样子

在我的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];
}
ios uiview screen transparent
5个回答
15
投票
- (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];
}

25
投票

请尝试使用这个

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

5
投票

在你的ViewController中,你可以说opaque是NO。比方说,您的视图名为myCustomView。

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

或者您可以转到故事板并取消选中不透明的复选框,例如-enter image description here


1
投票

在Swift 3.x中:(其中图层是视图)

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

0
投票

我为我的UIViewController做了半透明背景,就像它在这里推荐的一样,但是直到我将modalPresentationStyle设置为.custom它才能工作。

我的Swift 4代码:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.