IPhone - UIView addSubview顶部的差距 [英] IPhone - UIView addSubview Gap at top

查看:111
本文介绍了IPhone - UIView addSubview顶部的差距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个加载在MainWindow.xib中的视图。它只是一个带有uiimageview的视图,在整个屏幕上显示图像(320 X 480)。当应用程序加载时,我显示此视图,然后我执行

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];

标签栏控制器只是一个添加了2个视图控制器的UITabBarController。当它将tabbarController的视图添加到子视图时,它会在大约20px的顶部留下一个间隙。我的应用程序确实有一个状态栏,但这基本上是另一个的空间。除非我将此添加到我的视图控制器,否则会发生这种情况:

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;

但被告知我不应该这样做。所以现在我添加了一个子视图,但我不明白为什么我必须调整我的视图的CGRect以不显示20px。

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.

推荐答案

UITabBarController期望将其视图添加为UIWindow的子视图,而不是其他一些UIView的子视图。 frame属性定义了视图在其superview中的偏移量,因此UITabBarController实现默认情况下将其视图的帧偏移20个像素,为状态栏留出空间。您正在以非标准方式使用UITabBarController,方法是将其添加到状态栏已偏移20像素的视图中。 UITabBarController相对于其超视图将其视图偏移了20个像素,导致你看到的差距。

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.

解决这个问题的一个简单方法是将UITabBarController的视图添加为子视图窗口而不是视图:

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];

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

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

这篇关于IPhone - UIView addSubview顶部的差距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆