tags:

views:

34

answers:

1

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.

+1  A: 

The dimensions of the view of a view controller should not be changed. It should be autoresized to fit the size of the window or the parent controller.

If you really need a square view, make a subview.

// Note: better do this in -loadView or -viewDidLoad.
  UIView* container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
  [ts.view addSubview:container];
  [container release];
  // add stuff into the container view.
  // ...
KennyTM
will Apple reject my app if I manually resize the view of an view controller?
dontWatchMyProfile
@mystify: No. Just that it's troublesome for you to program with because the view controller may resize the view in some unknown way.
KennyTM