开发者

iOS View Controller Shifts After Rotation

开发者 https://www.devze.com 2023-01-27 09:13 出处:网络
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 o

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 cha开发者_开发百科nged 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号