0
votes

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.

1
NSLog(@"%s", __func__); will log the name of the current function or the signature of the current method. - Peter Hosey
awesome tidbit, thanks! I had been trying to figure out how to dump info to NSLog other than text strings. - David
Also, you may want to release the view you're assigning to self.view. You own it through the view property, and you also own it because you created it, so it is owned twice. If you never release that creation ownership, you will leak it. Conversely, because you didn't create the UIButton with alloc, you don't own that and should not release it. For further information, see the Memory Management Programming Guide: developer.apple.com/library/ios/documentation/Cocoa/Conceptual/… - Peter Hosey

1 Answers

0
votes

In your UIViewController's viewDidLoad method call [self.view addSubview:artistView] to add your custom view to the existing one. Then set it's size and position like normal with the frame property.