我创建了一个子类 UIView 的类。在drawRect中,我应该画一个橙色的矩形。然而它显示为黑色。
在我的自定义类中:
#import "testRect.h"
@implementation testRect
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)drawRect:(CGRect)rect
{
//// Color Declarations
UIColor* strokeColor = [UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 1];
UIColor* fillColor2 = [UIColor colorWithRed: 0.886 green: 0.59 blue: 0 alpha: 1];
//// Rectangle Drawing
UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(26.5, 171.5, 265, 139)];
[fillColor2 setFill];
[rectanglePath fill];
[strokeColor setStroke];
rectanglePath.lineWidth = 1;
[rectanglePath stroke];
}
还有我的视图控制器
#import "ViewController.h"
#import "testRect.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
testRect *test = [[testRect alloc] initWithFrame:CGRectMake(10, 10, 265, 139)];
[self.view addSubview:test];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end