UINavigationBar/Status Bar issue in IOS7
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.
Related videos on Youtube
StuartM
Web Developer/iOS Developer. Current Apps: Where to Next -https://itunes.apple.com/us/app/where-to-next/id884766874?ls=1&mt=8 Shows business data around your current location.
Updated on July 09, 2022Comments
-
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.
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:
Testing
- The plist setting is set to YES -
View controller-based status bar appearance
- 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 about 10 years"Extend Under Top Bars" should only affect your view's relationship with the Navigation bar.
-
erdekhayser about 10 yearsHave you tried moving the navigation bar's frame down by the height of the status bar?
-
StuartM about 10 yearsWhere should this be done though? Can you supply as an answer please?
-
Jayprakash Dubey about 10 yearsSame issue overhere...looking for solution.
-
user1010819 about 10 yearsDid 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 over 9 years@StuartM did you ever find a solution for this?
-
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 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.
- The plist setting is set to YES -
-
StuartM about 10 yearsThanks, 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 about 10 yearsAre you hiding the status bar while your modal view is visible?
-
jaredsinclair about 10 yearsWhat is the initial frame of your UINavigationController's view (not the navigation bar) in the NIB?
-
jaredsinclair about 10 yearsAre you using UINavgationBar directly, either in code or in your NIB? If so, why?
-
StuartM about 10 yearsThe 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 about 10 yearsI 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 about 10 years
-
StuartM about 10 yearsAny 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 about 10 yearsDon't include signatures in your posts.
-
StuartM about 10 yearsYes, I have tried adding this to my view controller viewDidLoad and my Navigation Controller subclass viewDidLoad but no change
-
StuartM about 10 yearsThe 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 about 10 yearsThis 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 about 10 yearsI 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 about 10 yearsI 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 about 10 years1. 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 about 10 yearsI have not yet been able to test this, I will get round to this soon and update
-
grigorievs about 10 yearsWhy you are asking people to help you if you aren't able to test(appreciate) someone's help?
-
StuartM about 10 yearsI 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 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 almost 10 yearsSure. 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 over 9 yearsmake 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 over 9 yearsUIBarPositioningDelegate 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 over 9 yearsNot true. There is a way. See my answer below. You can keep this bar behavior with a UIBarPositioningDelegate.
-
StuartM over 9 yearsNo 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 over 9 yearsCan you elaborate on this? AFAIK you can't set the delegate of a navigation bar when it's managed by a navigation controller.
-
jbcaveman over 9 yearsThis 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 over 8 yearsMy view controllers do not have a property called navigationBar.
-
Verdant over 8 yearsYou 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 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 about 8 yearsYour view controller must be the delegate of the bar you are trying to position with "positionForBar:", not the status bar.
-
malhal over 7 yearsFirstly the delegate type for the header is <UINavigationBarDelegate>, secondly this doesn't solve the problem.