Status bar appear over my view's bounds in iOS

2019-01-21 03:36发布

问题:

This question already has an answer here:

  • Status bar and navigation bar appear over my view's bounds in iOS 7 20 answers

I tried to test my app in iOS 7, and then I find my view appears over the status bar... As I read, in iOS 7, the status bar is hidden. So how do I make it compatible to run both in iOS 6 and iOS 7? Should I have to make different .xib files for different iOS versions for each screen?

I have been going through reading this: If both versions of a standard app should have a similar layout, use Auto Layout to create a UI that works correctly in both versions of iOS. To support multiple versions of iOS, specify a single set of constraints that Auto Layout can use to adjust the views and controls in the storyboard or XIB files (to learn more about constraints, see “Constraints Express Relationships Between Views”).

If both versions of a standard app should have a similar layout, and you’re not using Auto Layout, use offsets. To use offsets, first update the UI for iOS 7. Next, specify values that define the origin, height, and width of each element in the earlier UI as offsets from the element’s new position in the iOS 7 UI.

But when I use autolayout in .xib, it shows an error that autolayout is in a prior version to iOS 6.

How do I fix this problem?

回答1:

iOS 7 apparently supports the Status Bar being hidden for some views but not others. To hide it for all views, do the following:

  1. Make sure Hide during application launch is still checked, to support previous OS versions.
  2. In your Info.plist file, add View controller-based status bar appearance and set it to NO.
  3. You may need to "Clean" before building, (I did), but then your app should work as before: no status bar hanging over your views!


回答2:

You probably need to add the following code on each view controller.

- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)])
    {
        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
    else
    {
        // iOS 6
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    }
}

// Add this method
- (BOOL)prefersStatusBarHidden {
    return YES;
}


回答3:

I have the same issue. For now I made two hacks and will decide with which I'll go:

  • You can hide status bar completely by setting UIStatusBarHidden and UIViewControllerBasedStatusBarAppearance to true.
  • In my app, I created a Top Spacing constraint with value 0, and I programmaticaly change it to 20 if I detect that the app is running on iOS 7.

How do I make Autolayout account for the status bar area?


Well, I figured it out.

In your sub-view (BRSMyListSubViewController in my case), in viewDidLoad, you need to set one of these two

self.edgesForExtendedLayout = UIRectEdgeNone;
self.automaticallyAdjustsScrollViewInsets = NO;

OR

self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars = YES;

Interestingly enough, in the root view controller, these values are set to default UIRectEdgeAll, NO and YES respectively, but its tableView is NOT under navbar and footer.

I don't know why it's so illogical.

It's also strange that edgesForExtendedLayout has to be mixed with one of two other properties even though it's clearly responsible for the behavior.



回答4:

If you like to show a status bar under iOS 7 with Xcode 5, just rearrange the buttons and other subviews to make enough space around the status bar. But, just wait! I like to support iOS 6 too. How do I do that?

I found documentation from Apple, and found the "Supporting Two Versions of a Standard App" title in the document.

If you are editing an XIB file, select it and click assistant editor. You will able to find the "manual, automatic..." menu around there. Then choose "preview".

Then you'll find a view layout side by side. Then you will notice that there is a popup button around the right bottom of the right pane (view); it says "iOS 7 and later". You can choose "iOS 6.1 and earlier". Woa! Now you can make adjustments for iOS 6.1 without affecting the layout of iOS 7.

If you are working on the storyboard, it is basically the same. You choose a view controller object, and click assistant editor mode, choose "preview", then "iOS7 and later"... Bluh Bluh Bluh.

I'm not sure, but only the assistant editor is capable of switching to iOS 7+/iOS 6- mode. I just found this very recently, so please point out, if there are any misunderstandings or other tricks.



回答5:

There is no need to build multi-xib. I think your problem is the "20px": the same XIB file looks great in iOS 6, but it misses 20 pixels in iOS 7.

For example, you have a view, and it's Y = 0. In iOS 6, it's next to the bottom of the status bar. In iOS 7, it appears over the status bar.

You should use Xcode 5 to open your XIB files and enable assistant editor. Follow these steps:

  1. Select file inspector, and switch "View As" to "iOS 7 and later"

  2. Select size inspector, and fill deltaY with "-20"

  3. It's done!



回答6:

You just once need to check your main UIView size for iOS 7.0 & later & iOS 6.0 & lower, probably you will get idea.

From iOS 7 Apple has changed main view size = fixed It means if you add navigation bar, tabbar your view size remains same [iPhone 4s : 320 *480, iPhone 5 : 320 * 568].