Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.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 以编程方式删除UIButton背景色_Ios_Objective C_Uibutton - Fatal编程技术网

Ios 以编程方式删除UIButton背景色

Ios 以编程方式删除UIButton背景色,ios,objective-c,uibutton,Ios,Objective C,Uibutton,ios 7中的自定义UIButton子类有问题。我想做的是在nib文件中添加一个按钮,并在interface builder中设置其背景色。在IB中,我将其类设置为自定义UIButton子类:MyShapeButton。然后,在我的UIButton子类(MyShapeButton)中使用drawRect()函数绘制一个填充了该背景色的自定义按钮形状。我可以让所有这些工作,但真正的问题是,我不能删除按钮的原始背景色,使其透明。所以我的形状已绘制,但原始背景色使其模糊 请注意以下事项: 我的按钮类型

ios 7中的自定义UIButton子类有问题。我想做的是在nib文件中添加一个按钮,并在interface builder中设置其背景色。在IB中,我将其类设置为自定义UIButton子类:MyShapeButton。然后,在我的UIButton子类(MyShapeButton)中使用drawRect()函数绘制一个填充了该背景色的自定义按钮形状。我可以让所有这些工作,但真正的问题是,我不能删除按钮的原始背景色,使其透明。所以我的形状已绘制,但原始背景色使其模糊

请注意以下事项: 我的按钮类型设置为自定义,因为其他SO帖子说这很重要。这没有帮助

下面是我的子类中的drawRect代码:

- (void)drawRect:(CGRect)rect
{
    UIColor* buttonColor = self.backgroundColor;

//========= this doesn't work =========
    [self setBackgroundColor:[UIColor clearColor]];


    CGFloat width = rect.size.width;
    CGFloat height = rect.size.height;
    CGFloat radius = 10;
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(context, 0, height); //bottom left
    CGContextAddLineToPoint(context, 0, radius);
    CGContextAddArcToPoint(context, 0, 0, radius, 0, radius);
    CGContextAddLineToPoint(context, width, 0);
    CGContextAddLineToPoint(context, width, height-radius);
    CGContextAddArcToPoint(context, width, height, width-radius, height, radius);
    CGContextAddLineToPoint(context, 0, height);

    //shape is drawn here, but you can't see it:
    CGContextSetFillColorWithColor(context, buttonColor.CGColor);

    //uncomment this to see the button shape
    //CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);

    CGContextFillPath(context);
}
更新:这是
drawRect()的工作代码。

- (void)drawRect:(CGRect)rect
{
    CGFloat radius = 10.f;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds                                                  byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight
                                                       cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

我认为您应该使用另一个带有贝塞尔路径的
CALayer
来裁剪按钮背景。 参见此处的示例


您可以查看本教程。这里的问题不是“如何在按钮中绘制形状?”。问题是,我想使用interface builder中设置的背景色填充形状,然后删除原始背景色(填充矩形)。我可以在代码中手动设置颜色,但我希望它能自动使用IB背景色以自定义形状重新绘制按钮。然后,我不必在代码中设置颜色,这很烦人。你的回答让我走上了正确的轨道。作为面具的卡拉耶是一条路要走。谢谢我更新了上面的问题以显示我的新代码。