iOS 视图控制器旋转后移动

发布于 2024-10-04 21:47:25 字数 236 浏览 2 评论 0原文

我正在开发一个 iOS 应用程序。我的应用程序委托将我的视图控制器加载到窗口上并正确显示它,但有一个警告除外:我的视图控制器上的视图在 Y 轴上的距离不够远,并且状态栏夹住了我的顶视图。我更改了视图控制器的 XIB 文件来适应这一点,所以现在我的所有视图都从 Y 轴下方 20 像素处开始。初始加载时,一切看起来都很好。当我旋转时,我的视图比应有的低 20 像素(由于我的布局更改以适应第一次加载)。我想知道如何更好地处理视图控制器内的定位以处理状态栏高度。

I am working on an iOS application. My app delegate loads my view controller onto the window and displays it correctly, except for one caveat: the views on my view controller are not down far enough on the Y-axis and the status bar clips my top view. I changed my view controller's XIB file to accommodate for this, so now all my views start 20 pixels down the Y-axis. On initial load, everything looks fine. When I rotate, my views are 20 pixels lower than they should be (due to my layout change to accommodate the first load). What I'm wondering is how to better handle positioning within a view controller to handle the status bar height.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无名指的心愿 2024-10-11 21:47:33

您的解决方案取决于您创建项目的方式。

如果您在创建项目时使用了基于视图的应用程序模板,那么您将在 MainWindow.xib 中拥有视图控制器。如果您在该 NIB 中为视图控制器启用了状态栏属性(即选择灰色或黑色),那么您的视图控制器在最初(启动时)以及旋转设备后都应该可以正常工作。

如果您在创建项目时使用了基于窗口的应用程序模板,则必须将以下代码行添加到应用程序委托的 didFinishLaunchingWithOptions: 中

myViewController.view.frame = window.screen.applicationFrame;

,这会以某种方式神奇地正确设置内容。

Your solution depends on how you created the project.

If you used the View-Based Application template when you created project, then you'll have your view controller in the MainWindow.xib. Your view controller should work correctly both initially (on launch), as well as after rotating the devices, provided that you have the Status Bar attribute enabled (ie, selected Gray or Black) for your view controller in that NIB.

If you used the Window-Based Application template when you created the project, you'll have to add the following line of code to your app delegate's didFinishLaunchingWithOptions:

myViewController.view.frame = window.screen.applicationFrame;

which somehow magically gets things set up correctly.

羁客 2024-10-11 21:47:30

我通过不直接/以编程方式将视图渲染到窗口中来解决这个问题。相反,我所做的是创建一个 RootViewController,它在两个方向上都正确放置和对齐,并且在 RootViewController 内包含所有其他视图。

发生这种情况的原因是应用程序的窗口始终是整个屏幕(包括顶部的状态栏)。如果您尝试填充窗口,则必须处理并补偿状态栏和其他镶边。如果您管理根视图中的所有视图,则会更容易。

I've dealt with this problem by not rendering the view directly / programatically into the Window. What I do instead is create a RootViewController, which is placed and aligned correctly in both orientations, and have all other views inside the RootViewController.

The reason this happens is that the Window of an app is always the entire screen (including the status bar on top). If you try to fill the Window, you have to deal with and compensate for the status bar and other chrome. Makes it easier if you manage all views inside a root view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文