不知道为什么 UIView 被推高了大约 10px
我创建了一个简单的 iPhone 应用程序,其中有两个 .xib 文件。 在应用程序的应用程序委托中,确实完成了启动,我通过调用显示第一个 .xib 文件:
[window addSubview:myView];
并对 UIButton 的 IBAction 执行相同的操作,以将视图更改为 myView2。
我发现,当我运行应用程序时,两个视图都会出现一个大约 10 像素的白条。 我还注意到按钮偏移了 10 像素(大约)。 所以我的印象是视图是从屏幕外 10 个像素处显示的,并且结尾很短。
知道为什么会发生这种情况,如何修复它,或者如何进一步调试正在发生的事情? 我检查了我的 .xib 文件,它们消耗了设备的整个高度(即没有白条),所以这看起来是 XCode 内部的问题。
编辑:我已经缩小了问题的范围。 我使用两种方法来加载子视图。 当我在 applicationDidFinishLaunching 中加载第一个视图时,一切都很好。 但是,如果我用 [self originalView]
方法替换 window
的两条消息,那么一切都会变得有点混乱。
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//[self originalView]; <- I want to use this instead of the following two lines
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
-(void)endView{
endV = [[EndViewController alloc] initWithNibName:@"EndView" bundle:nil];
[window removeFromSuperview];
[window addSubview:endV.view];
}
-(void)originalView{
viewController = [[MyAppViewController alloc] init];
[window removeFromSuperview];
[window addSubview:viewController.view];
}
从我所看到的来看,我总是调用相同的代码行,无论它是在 applicationDidFinishLaunching
内部还是在 [self originalView]
中,但它看起来像 window
原来是一个不同的对象。
I've created a simple iPhone app which has two .xib files. In the app delegate at application did finish launching I display the first .xib file by calling:
[window addSubview:myView];
and I do the same on an IBAction for a UIButton to change the view to myView2.
What I'm finding is that there's a white bar of around 10 pixels when I run the app, for both views. I also notice that the buttons are offset by 10 pixels (approx.). So I get the impression that the view is being displayed from 10 pixels off the screen and ending short.
Any idea why this might be happening, how I can fix it, or how I can further debug what's going on? I've checked my .xib files and they are consuming the full height of the device (i.e. no white bars), so this looks to be a problem inside XCode.
EDIT: I've narrowed down the problem a little. I'm using two methods to load the subview. When I load the first view inside applicationDidFinishLaunching
everything is fine. However, if I replace the two messages to window
with the [self originalView]
method, then everything goes a bit haywire.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//[self originalView]; <- I want to use this instead of the following two lines
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
-(void)endView{
endV = [[EndViewController alloc] initWithNibName:@"EndView" bundle:nil];
[window removeFromSuperview];
[window addSubview:endV.view];
}
-(void)originalView{
viewController = [[MyAppViewController alloc] init];
[window removeFromSuperview];
[window addSubview:viewController.view];
}
From what I can see, I'm always calling the same lines of code, no matter if it's inside the applicationDidFinishLaunching
or in [self originalView]
but it seems like window
is turning out to be a different object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以正常方式使用 UIViewController 时(即 将其推到 UINavigationController 上),它会调整其视图的框架。
由于您是手动添加子视图,因此您必须自己调整框架。 原点位于右上角(状态栏顶部)。 您想要将视图向下移动 20 像素。
When using a UIViewController the normal way (i.e. pushing it on a UINavigationController), it adjusts its view's frame.
Since you're adding the subview manually, you have to adjust the frame yourself. The origin is in the upper right corner (at the top of the status bar). You want to shift your view 20 pixels down.
这是我的解决方案:
现在对我来说效果很好,祝你好运〜:)
Here is my solution:
It works fine for me now, good luck~ :)
在界面生成器中,您可以添加将以编程方式显示的模拟界面元素(例如状态栏或导航栏)。 在检查器中,选择您的视图,然后转到“属性”选项卡。 您可以在此处模拟状态栏、顶部栏或底部栏。
In interface builder, you can add Simulated Interface Elements that will appear programatically (such as the status bar or the navigation bar). In your inspector, select your view, and go to the Attributes tab. Here you can simulate The Status bar, a top bar, or a bottom bar.
看起来您在错误的位置对该
UIViewController
调用了 PushVC。 我有同样的问题。 通过将之前的UIViewController
中的[self.navigationController PushViewController:animated:]
从viewDidLoad
移动到viewDidAppear:
来解决这个问题> 在层次结构中。Looks like you call pushVC to this
UIViewController
in the wrong place. I had the same problem. It was solved by moving[self.navigationController pushViewController: animated:]
fromviewDidLoad
toviewDidAppear:
in a previousUIViewController
in hierarchy.每当出现奇怪的间隙,或者视图部分隐藏在状态栏后面时,它都与根控制器中的 shouldAutorotate 定义有关
我的应用程序中存在两个错误并解决了它们! 希望我可以节省你一些时间(我浪费了很多时间)
Whenever a weird gap appears, or the view is partly hidden behind the status bar, it has something to do with either
i had both bugs in my app and solved them! Hope I could save you some time (I wasted a lot on it)