0

I am new to iOS development, but have had an extensive crash course recently (BNR guid to iOS Programming). I am working on modally adding views to an app. I currently do not have a View Controller, so my AppDelegate's application:didFinishLaunchingWithOptions method looks as follows:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    MyUIView *map = [[MapSurface alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //this one does not work: MyUIView *map = [[MapSurface alloc] initWithFrame:CGRectMake(0, 0, 256, 256)];
    [map setBackgroundColor:[UIColor grayColor]];
    [[self window] addSubview:map];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

After the view loads, I make a call to add a subview, but I cannot set the frame size using CGRectMake. It doesn't show up on screen. The only thing I have found to work is using the screen size, but this is not what I want. Here is the code for adding this subview:

UIImage *image = [[UIImage alloc] initWithCGImage:img];//img is a CGImageRef
UIImageView *view = [[UIImageView alloc] initWithImage:image];
CGRect b = CGRectMake(0, 0, 256, 256);
//[view setFrame:b];//nothing shows on screen
//[view setBounds:b];//nothing shows on screen
//[view setBounds:[[UIScreen mainScreen] bounds]];//shows the tan area in the graphic below
[view setFrame:[[UIScreen mainScreen] bounds]];//fills the screen
[map addSubView:view];
CGImageRelease(img);

The tan area is not even filling the screen!

Phil
  • 35,852
  • 23
  • 123
  • 164
  • 1
    set image view's frame instead of bounds. Also, fetch window/view size after it is visible. – Swapnil Luktuke Oct 11 '12 at 02:49
  • @lukya, I changed that method call. Good catch. But I still have the issue of not being able to use `CGRectMake`. – Phil Oct 11 '12 at 02:54
  • where have you added the second section of code? and did you try setting the frame after adding the image view? – Swapnil Luktuke Oct 11 '12 at 02:59
  • Actually, it looks like the first suggestion is now working for CGRectMake now too - it was just that I was using bounds, not frame. Thanks for your help! – Phil Oct 11 '12 at 03:04
  • Setting the frame usually works a little better once it has been added to its parent view. – Thomas Denney Oct 11 '12 at 06:00

1 Answers1

0

I was setting the bounds attribute, when I should have been setting frame. Making this change fixed the issue.

Phil
  • 35,852
  • 23
  • 123
  • 164