Why is my view controllers view not quadratic?

Posted by mystify on Stack Overflow See other posts from Stack Overflow or by mystify
Published on 2010-03-27T19:44:42Z Indexed on 2010/03/27 19:53 UTC
Read the original article Hit count: 185

Filed under:
|

I created an UIViewController subclass, and figured out that the default implementation of -loadView in UIViewController will ignore my frame size settings in a strange way.

To simplify it and to make sure it's really not the fault of my code, I did a clean test with a plain instance of UIViewController directly, rather than making a subclass. The result is the same. I try to make an exactly quadratic view of 320 x 320, but the view appears like 320 x 200.

iPhone OS 3.0, please check this out:

UIViewController *ts = [[UIViewController alloc] initWithNibName:nil bundle:nil];
ts.view.frame = CGRectMake(0.0f, 0.0f, 320.0f, 320.0f);
ts.view.backgroundColor = [UIColor cyanColor];
[self.view addSubview:ts.view];

like you can see, I do this:

1) Create a UIViewController instance

2) Set the frame of the view to a quadratic dimension of 320 x 320

3) Give it a color, so I can see it

4) Added it as a subview.

Now the part, that's even more strange: When I make my own implementation of -loadView, i.e. if I put this code in there like this:

- (void)loadView {
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 320.0f)];
    v.backgroundColor = [UIColor cyanColor];
    self.view = v;
    [v release];
}

then it looks right.

Now lets think about that: In the first example, I do pretty much exactly the same, just that I let UIViewController create the view on it's own, and then take it over in order to change it's frame. Right?

So why do I get this strange error? Right now I see no other way of messing around like that to correct this wrong behavior. I did not activate anything like clipsToBounds and there's no other code touching this.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about iphone-sdk