UIView Clipped By Statusbar until Autorotate

2019-04-30 06:26发布

Ive created a multiview application that uses multiple controllers to display and control views. The problem Im having is, when the simulator initially loads the view the header is partially covered by the bar at top of screen and the tool bar at the base is not touching the base of the screen. I used the Interface builder size attributes to control the view when the iphone rotates and it works perfectly. All smaps into place perfectly both in landscape and portrait mode AFTER a rotation but the problem is with the initial load before a rotation occurs. Your thoughts a much appreciated. Tony

4条回答
戒情不戒烟
2楼-- · 2019-04-30 06:36

The problem is that you're adding your controller "incorrectly" according to Apple docs (although IMHO Apple designed it badly - the default should be that you don't need to shift!)

if you're going to have a status bar, Apple requires that you "manually" shift all your controllers down by 20 pixels (more accurately, by the height of the statusbar - although that's always 20 pixels today, Apple lets you request the height at runtime, from the "statusBarFrame" property in UIApplication)

Apple's classes - e.g. UINavigationController / UITabBarController - automatically shift themselves down by 20 pixels when they're added to the screen. Both classes have a bug where they will do this shift even if they are not the main controller - so your app suddenly shifts down an extra 20 pixels, leaving 20 pixels of white space at top.

But, when they rotate, those classes often "get it right" and move back into place. Or vice versa.

c.f. this link for a much more detailed explanation of what's going on, and how to write your code the way Apple wants you to:

http://blog.red-glasses.com/index.php/tutorials/iphone-auto-rotation-for-intermediate-developers-tutorial-1/

查看更多
Bombasti
3楼-- · 2019-04-30 06:44

I grappled with this issue for days and days--no amount of fiddling in IB worked.

Ultimately I got it to work by adding this line:

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

to the application:didFinishLaunchingWithOptions: method. (Where mainViewController is the primary UIViewController).

查看更多
神经病院院长
4楼-- · 2019-04-30 06:46

I ran into this issue too. Specifically, when displaying an ADBannerView, my whole view would shift and be under the status bar and leave a little empty space just the size of the status bar at the bottom of the iPhone screen. Here's how I solved it : (Adam's answer here helped me figure this out):

// In the function that displays an iAD Banner
CGRect contentFrame = self.view.bounds;
CGRect myStatusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGFloat statusHeight = myStatusBarFrame.size.height;

// Set the view's origin to be under the status bar.

contentFrame.origin.y = statusHeight;

I needed to set the origin of my view to be below the Status bar, and that solved the issue for me.

查看更多
时光不老,我们不散
5楼-- · 2019-04-30 06:53

I've had issues with views being clipped by status, nav, and tab bars. I've fixed most of them by using the Simulated Metrics feature in Interface Builder. That way what your laying out in IB is a lot more accurate to what your actually going to get.

查看更多
登录 后发表回答