UINavigationBar/Status Bar issue in IOS7

64,336

Solution 1

Did you try Apple recommendation about "Preventing the Status Bar from Covering Your Views": https://developer.apple.com/library/content/qa/qa1797/_index.html

And did you have a look at "UIBarPositioningDelegate": https://developer.apple.com/documentation/uikit/uibarpositioningdelegate

Solution 2

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

Solution 3

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; 
}

Solution 4

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 hiddden then doing

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

in the menu's view controller.

Solution 5

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.

Share:
64,336

Related videos on Youtube

StuartM
Author by

StuartM

Web Developer/iOS Developer. Current Apps: Where to Next -https://itunes.apple.com/us/app/where-to-next/id884766874?ls=1&amp;mt=8 Shows business data around your current location.

Updated on July 09, 2022

Comments

  • StuartM
    StuartM over 1 year

    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
    
    • Greg M. Krsak
      Greg M. Krsak about 10 years
      "Extend Under Top Bars" should only affect your view's relationship with the Navigation bar.
    • erdekhayser
      erdekhayser about 10 years
      Have you tried moving the navigation bar's frame down by the height of the status bar?
    • StuartM
      StuartM about 10 years
      Where should this be done though? Can you supply as an answer please?
    • Jayprakash Dubey
      Jayprakash Dubey about 10 years
      Same issue overhere...looking for solution.
    • user1010819
      user1010819 about 10 years
      Did you find any solution? Same problem for me , when I print navigationBar frame for first controller it prints (x,y)->(0,20) then when I push a view controller it prints (x,y)->(0,0). How did it become 0?
    • mattsven
      mattsven over 9 years
      @StuartM did you ever find a solution for this?
    • StuartM
      StuartM over 9 years
      @mattsven - No we had to take a different approach due to time constraints. We left the SO question open as all the answers are relevant and workarounds to the issue. Thanks
    • mattsven
      mattsven over 9 years
      @StuartM Yeah, the fix was actually really simple, involved manipulating centerY - navBar.centerY += 20, when fading out the status bar, and that's it.
  • StuartM
    StuartM about 10 years
    Thanks, I have updated the question with a further EDIT. What I am finding is the frame changes for the navbar. Initially it loads as 0,20 offset and then later changes to be 0,0. When checking new apps on the app store they have the layout as 0,20 through out. Is there no easy way to accomplish this. Ideally I do not want to have to animate the navbar/other views down each time it is out of line
  • jaredsinclair
    jaredsinclair about 10 years
    Are you hiding the status bar while your modal view is visible?
  • jaredsinclair
    jaredsinclair about 10 years
    What is the initial frame of your UINavigationController's view (not the navigation bar) in the NIB?
  • jaredsinclair
    jaredsinclair about 10 years
    Are you using UINavgationBar directly, either in code or in your NIB? If so, why?
  • StuartM
    StuartM about 10 years
    The navigation controllers view frame remains the same through out. nav view frame: {{0, 0}, {320, 480}}. I am not using navigation bar directly it is used from a navigation controller
  • StuartM
    StuartM about 10 years
    I do not think your answer is the solution here. It appears my navigation bar frame is being changed somehow but I cannot tell how/why. Your answer says that you cannot get the same affect as iOS6. What I am trying to achieve is the status bar as an item so that the navigation bar appears 20 down from the top and as one item together. Like most apps I see on the app store that have made themselves ios7 compat.
  • StuartM
    StuartM about 10 years
  • StuartM
    StuartM about 10 years
    Any thoughts following on from the above. I have commented with the relevant frame information. Your answer says that you cannot get the same affect as iOS6. What I am trying to achieve is the status bar as an item so that the navigation bar appears 20 down from the top and as one item together. Like most apps I see on the app store that have made themselves ios7 compat
  • JRG-Developer
    JRG-Developer about 10 years
    Don't include signatures in your posts.
  • StuartM
    StuartM about 10 years
    Yes, I have tried adding this to my view controller viewDidLoad and my Navigation Controller subclass viewDidLoad but no change
  • StuartM
    StuartM about 10 years
    The edgesForExtendedLayout affects how the view controller is actually displayed with the status bar, wether the content will go underneath etc. I have put in my question that I had already tried this too.
  • StuartM
    StuartM about 10 years
    This reference says 'Applications that use an opaque UINavigationController or UITabBarController automatically keep their content below the status bar.' but this is clearly not true by my example/question. I have also tried to set the bar positioning as top and topattached to no avail.
  • Moebius
    Moebius about 10 years
    I used the method covered in the "Preventing the Status Bar from Covering Your Views" that sets up a vertical space constraint to the top layout guide and the UINavigationBar that I put on my view is placed below the status bar. I got the same result by pinning the UINavigationBar with 20 pixels of vertical space from the top of the view.
  • StuartM
    StuartM about 10 years
    I have tried the resolution to this question, but had no luck. Can you provide a code example of how you set out the AutoLayout, or did you compete in IB?
  • grigorievs
    grigorievs about 10 years
    1. Add outlet for Bar Top Space Constraint (Vertical Space - Navigation Bar - View) in the code. 2. Set delegate for toolbar to the according VC. 3. Add de delegate method as specified in the url above. 4. Add this one in viewWillAppear > if (IS_IOS_7) self.navBarTopSpaceConstraint.constant = 20;
  • StuartM
    StuartM about 10 years
    I have not yet been able to test this, I will get round to this soon and update
  • grigorievs
    grigorievs about 10 years
    Why you are asking people to help you if you aren't able to test(appreciate) someone's help?
  • StuartM
    StuartM about 10 years
    I had to use a different approach as I was unable to get this resolved, I will re-open the project later today and try to get this to work. I beleive the issue lies with how the third party slide menu works as this was also reported to them and they were unable to resolve. But the question does provide a vast variety of information for other people with this issue to help try and resolve for them.
  • onmyway133
    onmyway133 almost 10 years
    @SunilAdhyaru "First, "view as iOS 6.0", then set a delta of "20" to achieve the +20 offset in iOS 7" Can you explain this ?
  • Sunil Adhyaru
    Sunil Adhyaru almost 10 years
    Sure. Basically you have to set view as a iOS 6.0 instead of 7.0. You can see it in "Interface Builder Document" item of Interface Builder.
  • brian.clear
    brian.clear over 9 years
    make sure you set up AutoLayout correctly for the whole viewcontroller. The apple doc just says fix the first one. I only applied constraint between Top Layout Guide and top view and no other and it didnt work. But worked once Id added constraints to all view below viewcontroller.view at least and cleared all layout warning in IB
  • jbcaveman
    jbcaveman over 9 years
    UIBarPositioningDelegate WORKS LIKE A CHARM! Just make your view controller a UIBarPositioningDelegate and assign it as the bar's delegate. Then add this method to your view controller (only available in iOS7+): - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar { return UIBarPositionTopAttached; }
  • jbcaveman
    jbcaveman over 9 years
    Not true. There is a way. See my answer below. You can keep this bar behavior with a UIBarPositioningDelegate.
  • StuartM
    StuartM over 9 years
    No we had to take a different approach due to time constraints. We left the SO question open as all the answers are relevant and workarounds to the issue. Thanks
  • Alexis
    Alexis over 9 years
    Can you elaborate on this? AFAIK you can't set the delegate of a navigation bar when it's managed by a navigation controller.
  • jbcaveman
    jbcaveman over 9 years
    This solution works when adding a custom navigation bar to a UIViewController. For a UINavigationController, you will need to create a subclass of UINavigationController and define that subclass as a UINavigationBarDelegate (which conforms to UIBarPositioningDelegate), similar to this: stackoverflow.com/a/7453933/485895. Just adding the positionForBar: method mentioned in my answer is all you need to do to solve the problem in question, but you can see that this subclass approach offers you other opportunities as well.
  • Chris Prince
    Chris Prince over 8 years
    My view controllers do not have a property called navigationBar.
  • Verdant
    Verdant over 8 years
    You can access it with this, but it doesn't solve the problem self.navigationController.navigationBar.delegate = self; If you success to solve the problem with this, tell me plz. It makes me crazy ><
  • Daniel
    Daniel about 8 years
    @JoelCave How do I assign it as the bar's delegate. Does the status bar have a delegate? or do you mean the UINavigationBar?
  • jbcaveman
    jbcaveman about 8 years
    Your view controller must be the delegate of the bar you are trying to position with "positionForBar:", not the status bar.
  • malhal
    malhal over 7 years
    Firstly the delegate type for the header is <UINavigationBarDelegate>, secondly this doesn't solve the problem.