Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIBezierPath简单矩形_Ios_Objective C_Core Graphics - Fatal编程技术网

Ios UIBezierPath简单矩形

Ios UIBezierPath简单矩形,ios,objective-c,core-graphics,Ios,Objective C,Core Graphics,我只想使用以下函数为视图绘制一个简单的矩形: - (void)drawRect:(CGRect)rect { [super drawRect:rect]; if (self.drawTextBouble) { [[UIColor blueColor] setFill]; UIBezierPath *aPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(40, 0, 230, 120)

我只想使用以下函数为视图绘制一个简单的矩形:

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    if (self.drawTextBouble) {
        [[UIColor blueColor] setFill];
        UIBezierPath *aPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(40, 0, 230, 120) cornerRadius:12.0];
        [aPath fill];
    }
}
上面的代码用纯黑色背景填充视图,矩形外是不透明的。我怎样才能解决这个问题

编辑:

下面的解决方案可行,但这也可行:

[self setOpaque:NO];

您的绘图代码是正确的。如果希望自定义绘制的视图具有透明背景,只需设置

self.backgroundColor = [UIColor clearColor];
在视图的
-(id)initWithFrame:(CGRect)frame

编辑:只是关于调用
[super-drawRect:rect]
的一点提示<代码>UIView文档说明:

如果直接将
UIView
子类化,则此方法的实现不需要调用super。但是,如果您正在对不同的视图类进行子类化,那么应该在实现的某个时候调用super


这是一个比我想象的更好的问题例子。存在错误的代码、问题的解释和期望的结果。真遗憾,我不知道答案!我无法用你的代码重现问题,但也许你不应该调用
[super-drawRect:rect]
,比较。谢谢你的评论:),我尝试过,但行为相同。@flatronka:你确定视图的背景色设置为透明(“clearColor”)?谢谢,这是有效的,我找到了另一个解决方案,也很有效。