views:

3573

answers:

2

I have a view that is loaded in the MainWindow.xib. It is just a view with a uiimageview in it that shows a image on the entire screen ( 320 X 480 ). When the app loads I display this view and then I do a

[self.view addSubview:tabbarController.view];

Tab Bar Controller is just a UITabBarController with 2 View Controllers added to it. When it adds the tabbarController's view to the subview it leaves a gap at the top of about 20px. My app does have a status bar but this is basically room for another. This happens unless I add this to my view controller:

self.view.frame = CGRectMake(0, 0, 320, 480);

Can anyone explain this. I was doing

self.view = tabbarController.view;

but was told I shouldn't do that. So now I'm adding a subview, but I don't understand why I have to adjust the CGRect of my view to not show the 20px.

A: 

http://stackoverflow.com/questions/1186492/1195812#1195812 this is a link to a similar question, it might help you

Daniel
I tried going through and making sure all views were 460 in the IB and I still get the gap.
Brian
Check out my answer on that, what i did to fix it when i h ad the problem is different
Daniel
+3  A: 

UITabBarController expects to have its view added as a subview of UIWindow, not as a subview of some other UIView. The frame property defines the offset of the view within its superview, so the UITabBarController implementation offsets its view's frame by 20 pixels by default to leave room for the status bar. You're using UITabBarController in a nonstandard way by adding it to a view that's already been offset by 20 pixels for the status bar. UITabBarController offsets its view by an additional 20 pixels relative to its superview, causing the gap you see.

One clean way to fix this is add the UITabBarController's view as a subview of the window instead of a view:

[[[UIApplication sharedApplication] keyWindow] addSubview:tabbarController.view];

(Note: The keyWindow method will only return your window if you've already called makeKeyAndVisible. Otherwise, you may want to set a window property on your UIViewController.)

cduhn
Thanks for clear explanation. Maybe I should go back and restructure part of my app. I want to do things the correct way, but I thought it may be better to separate the view controller logic from the app delegate - so my root view controller does all that it needs. That would explain why in my root view controller if I did self.view = tabbarController.view instead of adding as a subview, I wouldn't get the gap. Great answer. I'd vote up more if I could.
Brian
It sounds like your instinct was good here. This is just one of those design limitations in Cocoa Touch that we have to work around for now. Bear in mind though that you don't necessarily need add the tabbarController.view to your window from your app delegate. You can add it in your view controller if that's where you think it makes the most sense. You just need to have access to the window object, either via the keyWindow method, or by setting it as a property on your controller back in applicationDidFinishLaunching:.
cduhn