0

I have a UIControl subclass where I have drawRect implemented. I am trying to draw a rectangle inside of the drawRect method using a UIBezierPath. All the appropriate methods are being called, nothing is null and the app runs with no issues. The problem is, nothing but the frame is drawn. Meaning, I can see the area of the frame, because it is black, but I cannot see anything that was drawn onto the frame.

Here is what I am doing to instantiate the class:

ZHButton *button =
    [[ZHButton alloc] initWithFrame:CGRectMake(100, 200, 100, 50)];
button.buttonType = NSSelectorFromString(LEFT_ROUNDED_CORNER_BUTTON);
[self.view addSubview:button];

Here is the drawRect: method, which calls different methods using a selector:

- (void)drawRect:(CGRect)rect {
    UIBezierPath *button = [self performSelector:self.buttonType withObject:nil];
    [[UIColor orangeColor] setFill];
    [button fill];
}

- (UIBezierPath *)drawButtonWithLeftCornersRounded {
        return [UIBezierPath bezierPathWithRoundedRect:self.frame
                                     byRoundingCorners:UIRectCornerTopLeft |
                                                       UIRectCornerBottomLeft
                                           cornerRadii:buttonRadii];
}

Everything appears to be working fine, but the UIBezierPath does not get drawn. Also, not sure if using a selector is the best way to do this. Anyone know what is going on with this?

4

2 回答 2

0

It seems you're not calling the drawButtonWithLeftCornersRounded method. I believe this line is wrong:

[self performSelector:self.buttonType withObject:nil];

But it should be:

[self performSelector:@selector(drawButtonWithLeftCornersRounded) withObject:nil];

Hope this helps!

于 2013-09-23T19:34:22.493 回答
0

解决了。在drawButtonWithLeftCornersRounded bezierPathWithRoundedRect不应该self.frame。这将导致 x 和 y 坐标位于框架内的 x、y 位置,而不是视图。我将它的参数更改为:

CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
于 2013-09-23T20:04:01.660 回答