NavigationController and autoresizing Views


#1

Hi,

I have a question to the autoresizing behavior of NavigationController. I started an own App based on a Single View Application (with ARC, without storyboards). So I get a ViewController with a ViewController.xib. Then I implemented a NavigationController with the ViewController as RootViewController:

[code]- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@“ViewController” bundle:nil];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[[navController navigationBar] setTintColor:[UIColor darkGrayColor]];
[[self window] setRootViewController:navController];
[[self window] makeKeyAndVisible];
return YES;

}[/code]

I expected implementing the ViewController as RootViewController of the NavigationController will automatically autoresize the ViewControllers view. But the ViewControllers view is still 320 x 480 pixels. But why? Hope somebody ca help me. Thanks a lot.

Frank (Germany)


#2

I am trying to understand the problem you are facing.

How did you get this 320 x 480 size? If you are creating an iPhone single-view app, should not the view size be 320 x 460?


#3

You are right. My View is 320 x 460 pixels.


#4

I try to explain my problem a bit more in detail.

In viewDidLoad of my ViewController I added a UIImageView as subview. I set the center property of my UIImageView so that the UIImageView is right in die Center of the view of ViewController (wheel is my UIImageView):

wheel.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2); [self.view addSubview:wheel];

Without the NavigationController my UIImageView “wheel” is in die center of the view of ViewController. But when I implemented my NavigationController it isn’t.

When I debug the bounds of my view of ViewController with NSLog it says the view is still 320 x 460 pixels. But why?

- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"width %f, self.view.bounds.size.width"); NSLog(@"height %f, self.view.bounds.size.height");

It’s the same when I put some UI elements in ViewController.xib at the bottom of the view and set die autoresizing anchors to upper left and right. But why?


#5

That’s probably because, you are looking at the bounds rectangle of the superview, the view provided by your view controller. Since you are adding your image view (wheel) to the superview, should you not be looking at the bounds rectangle of your image view, instead?

NSLog (@"%@", NSStringFromCGRect (wheel.bounds)); NSLog (@"%@", NSStringFromCGRect (wheel.frame));
Adding child views to a view will not affect the size of that view, the superview.


#6

I found the problem. I set die center property in viewDidLoad instead of viewWillAppear. But in viewDidLoad the view of ViewController is not autoresized.

Now I set the center property in viewWillAppear and it works.

Thanks a lot for your support.


#7

You may want to set the autoresize mask of the UIImageView programmatically in viewDidLoad. My guess is that if you rotate this view controller, it’ll end up in some corner of the screen.