UINavigationBar/Status Bar issue in IOS7

2019-01-21 01:51发布

Final EDIT

(Rather than having an overly long question with edits making a final edit for clarification, please see other edits if needed).

Controller Setup

I have an application that is setup as follows:

InitialViewController (subclass of ECSlidingViewController)

Main Navigation Controller (subclass of UINavigationController)

Main Home View Controller (subclass of UIViewController)


In the viewDidLoad of the initialViewController I load the main navigation controller in with the Home View Controller as its root.

self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MainNavVC"];

The Issue

On the first load of the application the status bar and navigation bar are seperated. enter image description here

This is the desired effect.

However, I then load a modal view controller and close it, using the standard methods:

[self performSegueWithIdentifier:@"LoadSelectOpponentVC" sender:self];

Then close with:

[self dismissViewControllerAnimated:YES completion:nil];

This in turn causes the main navigation controller (holding the home view controller) to display the status bar incorrectly and overlapping:

enter image description here

Testing

  1. The plist setting is set to YES - View controller-based status bar appearance
  2. I have tried setting the edgesForExtendedLayout to the relevant none, but no change.

Logging

I have tried to log out some frames to see where the issue occurs:

On first Load:

Main Nav VC - View Frame - {{0, 0}, {320, 480}}

Main Nav VC - Nav Bar Frame - {{0, 0}, {320, 44}}

Initial VC - View Frame - {{0, 0}, {320, 480}}

Home VC - View Frame - {{0, 0}, {320, 480}} -- viewDidLoad Home VC

Home VC - View Frame - {{0, 64}, {320, 416}} -- viewWillAppear Home VC

--- After Modal is opened/closed ----

Home VC - View Frame - {{0, 64}, {320, 416}} -- viewWillAppear Home VC

Main Nav VC - View Frame - {{0, 0}, {320, 480}} -- viewWillAppear Main Nav

Main Nav VC - Nav Bar Frame - {{0, 20}, {320, 44}} -- viewWillAppear Main Nav

Home VC - View Frame - {{0, 44}, {320, 436}} -- viewDidAppear Home VC

15条回答
Explosion°爆炸
2楼-- · 2019-01-21 02:19

I fixed such a problem using answer from this post: iOS 7 | Navigation bar / Toolbar buttons very close to status bar

Using Autolayout you should ignore setting a new Frame. You should add a Top Space Constraint equal to 20 for the TopBar for iOS 7.

查看更多
时光不老,我们不散
3楼-- · 2019-01-21 02:21

Did you try to add the following code to your viewDidLoad method :

if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
    self.edgesForExtendedLayout = UIRectEdgeNone;

It is quickly explained in Apple migrating to iOS 7 doumentation.

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-21 02:21

I have answered this problem at length in this answer to a similar question. The short answer is this: there is no way to get the automatic status bar layout behavior you're used to from iOS 6 and earlier. You'll have to design around it, or find a way to simulate the old style (I cover both approaches).

I strongly advise you not to make manual adjustments to the navigation bar frame. Let UINavigationController handle that yourself. Most likely, your problem is that that your navigation controller's view's frame isn't equal to the UIScreen's bounds.

查看更多
倾城 Initia
5楼-- · 2019-01-21 02:21

I know that you have ViewController as Main VC. But if someone is using UITableviewController and having the same problem, this code solves the issue:

self.tableView.contentInset = UIEdgeInsetsMake(20.0f, 0.0f, 0.0f, 0.0f);
查看更多
Rolldiameter
6楼-- · 2019-01-21 02:21

the best way is by adding this in the view that you have the problem with:

if (self.navigationController.navigationBar.frame.origin.y==0)
        self.navigationController.navigationBar.frame = 
            CGRectMake(self.navigationController.navigationBar.frame.origin.x, 
                       self.navigationController.navigationBar.frame.origin.y, 
                       self.navigationController.navigationBar.frame.size.width, 
                       64);
查看更多
Luminary・发光体
7楼-- · 2019-01-21 02:23

I'm a little too late to the party, but since I faced the same issue and this was the first result that showed up in the search, I guess my answer could help other people :)

I fixed the problem by implementing

- (BOOL)shouldAutorotate
{
    return NO;
}

in the view controller that presents the modal.

查看更多
登录 后发表回答