2013-09-23 24 views
0

我有一個UIControl子類,其中我已經實現了drawRect。我正在嘗試使用UIBezierPathdrawRect方法中繪製一個矩形。所有適當的方法都被調用,沒有任何東西是空的,應用程序運行時沒有問題。問題是,沒有什麼,但畫框。意思是,我可以看到框架的區域,因爲它是黑色的,但我看不到畫在框架上的任何東西。drawRect:被調用,只顯示框架沒有圖紙

下面是我在做什麼來實例化類:

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

這裏是drawRect:方法,它調用使用選擇不同的方法:

- (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]; 
} 

一切似乎都工作正常,但UIBezierPath沒有繪製。另外,不確定是否使用選擇器是最好的方法。任何人都知道這是怎麼回事?

回答

0

解決它。在drawButtonWithLeftCornersRoundedbezierPathWithRoundedRect不應該是self.frame。這將導致x和y座標位於框架內的x,y位置,而不是視圖。我將其參數更改爲:

CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height); 
0

看來你並沒有打電話給drawButtonWithLeftCornersRounded方法。我相信,這條線是錯誤的:

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

但它應該是:

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

希望這有助於!

+0

drawButtonWithLeftCornersRounded正在被調用。 self.buttonType是一個選擇器。我嘗試了你的改變,它有相同的結果。 – tentmaking