我想在我的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];
}
- (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];
}
请尝试使用这个
view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
view.opaque = NO;
在Swift 3.x中:(其中图层是视图)
layer.backgroundColor = UIColor.clear.cgColor
layer.isOpaque = false
我为我的UIViewController做了半透明背景,就像它在这里推荐的一样,但是直到我将modalPresentationStyle设置为.custom它才能工作。
我的Swift 4代码:
modalPresentationStyle = .custom
view.isOpaque = false
view.backgroundColor = UIColor.black.withAlphaComponent(0.2)
我使用present(控制器,动画,完成)方法从另一个打开我的控制器。