简体   繁体   English

IPhone - UIView addSubview顶部的差距

[英]IPhone - UIView addSubview Gap at top

I have a view that is loaded in the MainWindow.xib. 我有一个加载在MainWindow.xib中的视图。 It is just a view with a uiimageview in it that shows a image on the entire screen ( 320 X 480 ). 它只是一个带有uiimageview的视图,在整个屏幕上显示图像(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. 选项卡栏控制器只是一个UITabBarController,添加了2个视图控制器。 When it adds the tabbarController's view to the subview it leaves a gap at the top of about 20px. 当它将tabbarController的视图添加到子视图时,它会在大约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. 所以现在我正在添加一个子视图,但我不明白为什么我必须调整我的视图的CGRect以不显示20px。

UITabBarController expects to have its view added as a subview of UIWindow, not as a subview of some other UIView. UITabBarController期望将其视图添加为UIWindow的子视图,而不是其他一些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. frame属性定义了视图在其superview中的偏移量,因此UITabBarController实现默认情况下将其视图的帧偏移20个像素,为状态栏留出空间。 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,方法是将其添加到状态栏已偏移20像素的视图中。 UITabBarController offsets its view by an additional 20 pixels relative to its superview, causing the gap you see. UITabBarController相对于其超视图将其视图偏移了20个像素,从而导致您看到的间隙。

One clean way to fix this is add the UITabBarController's view as a subview of the window instead of a view: 解决此问题的一种简洁方法是将UITabBarController的视图添加为窗口的子视图而不是视图:

[[[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.) (注意:如果你已经调用了makeKeyAndVisible,keyWindow方法将只返回你的窗口。否则,你可能想在你的UIViewController上设置一个窗口属性。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM