Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPhone - UIView addSubview Gap at top

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.

like image 561
Brian Avatar asked Aug 31 '09 16:08

Brian


1 Answers

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.)

like image 77
cduhn Avatar answered Oct 31 '22 16:10

cduhn