Jovan
Jovan

Reputation: 2670

UIWindow bounds/frame is null

I'm creating a ios application without nib files and want to initialize the window in the application delegate like so:

CGRect screenBounds = [[UIScreen mainScreen] bounds];
self.window = [[[UIWindow alloc] initWithFrame:screenBounds] autorelease];
self.window.backgroundColor = [UIColor greenColor];

NSLog(@"%@", self.window.bounds);

The window shows up and I can see the green color I defined. When I debug self.window I get to see the bounds of the frame I just defined but when I want to access the bounds property, it returns null. Same with frame. What's the difference?

When I want to initialize a view next, it won't work because the frame/bounds are not defined to init that view:

// create root view
RootView* rootView = [[[RootView alloc] initWithFrame:self.window.bounds] autorelease];

What am I doing wrong?

Upvotes: 3

Views: 5120

Answers (2)

beryllium
beryllium

Reputation: 29767

You should use format specifiers for NSLog frame:

NSLog(@"x=%.1f, y=%.1f, width=%.1f, height=%.1f", 
      self.window.bounds.origin.x, 
      self.window.bounds.origin.y, 
      self.window.bounds.size.width, 
      self.window.bounds.size.height);

Upvotes: 0

deanWombourne
deanWombourne

Reputation: 38475

Yea, this is not going to work

NSLog(@"%@", self.window.bounds);

The window's bounds are not an object so %@ can't be used to display it :)

Try converting the CGRect into an NSString * first :

NSLog(@"%@", NSStringFromCGRect(self.window.bounds));

That should give you a bit more debugging information to help you solve your problem :)

NB %@ simply calls description for the object and displays that.

Upvotes: 6

Related Questions