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条回答
该账号已被封号
2楼-- · 2019-01-21 02:25

I'm surprised no one has hit on the right answer yet. UIBarPositioningDelegate works like a charm! Just make your view controller a UIBarPositioningDelegate and assign it as the bar's delegate. Position the bar 20 pixels from the top of your view. Then add this method to your view controller (only available in iOS7+):

- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar 
{ 
   return UIBarPositionTopAttached; 
}
查看更多
Emotional °昔
3楼-- · 2019-01-21 02:28

I had a similar issue with a "hamburger" menu button that slide the main view controller over and had a menu view controller on the side. I found that the menu view controller's navigation bar was not aware if the status bar was shown or not. I fixed it by posting a notification when the status bar was shown and hiffffden then doing

[self.navigationController setNavigationBarHidden:YES/NO animated:NO];

in the menu's view controller.

查看更多
小情绪 Triste *
4楼-- · 2019-01-21 02:29

In iOS 7.0 UIViewController works by default this way. View will be full screen if you are using UIViewController inside UINavigationController and the navigationBar is visible.

If navigationBar is visible do following. ==>

self.edgesForExtendedLayout = UIRectEdgeNone

if navigationBar is hidden do following . ==>

Adjust all the UIView elements by shifting 20 points

If you use Interface builder, you can use iOS6/7 deltas: First, "view as iOS 6.0", then set a delta of "20" to achieve the +20 offset in iOS 7

查看更多
登录 后发表回答