I’m just getting into iOS development. I’m tinkering with core graphics, and I’m trying to figure out how to both limit the size of a drawing, and place it at specific coordinates on the screen. Here is my HomeViewController.m loadView AND addCircle method, and my Artist.m drawRect method:
HomeViewController.m -> loadView
- (void)loadView
{
NSLog(@"HomeViewController.m : loadView");
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UILabel *lbl;
lbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 50, 220, 70)];
lbl.text = @"Home Screen";
lbl.font = [UIFont boldSystemFontOfSize:30];
[self.view addSubview:lbl];
[lbl release];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark ];
infoButton.frame = CGRectMake(10, 10, 300, 300);
[self.view addSubview:infoButton];
[infoButton addTarget:self action:@selector(loadInfo) forControlEvents:UIControlEventTouchUpInside];
[infoButton release];
[self addCircle];
// self.view = [[[Artist alloc] init] autorelease];
}
HomeViewController.m -> addCircle
- (void)addCircle
{
Artist *artist = [[[Artist alloc] init] autorelease];
[self.view addSubview:artist];
// [artist drawRect:CGRectMake(100, 100, 100, 100)];
}
Artist.m -> drawRect
- (void)drawRect:(CGRect)rect
{
NSLog(@"Artist.m : drawRect");
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef red = [[UIColor redColor] CGColor];
CGContextFillRect(context, CGRectMake(130,200,120,120));
CGContextSetFillColorWithColor(context,red);
CGContextFillEllipseInRect(context, CGRectMake(130, 200, 120, 120));
}
Essentially, I’m trying to avoid having the drawing take up the whole screen, but rather be limited to the size that I make it and sit on top of the other views.
In your
UIViewController‘sviewDidLoadmethod call[self.view addSubview:artistView]to add your custom view to the existing one. Then set it’s size and position like normal with theframeproperty.