UINavigationController bar covers its uiviewcontroller's content
Asked Answered
H

4

15

I have a UIViewController, MyViewController, with a UIToolbar at the top. Using interface builder the doc outline looks like this:

View 
  - subview1
  - subview2
  - UIToolbar
     - Bar Button Item1
     - Bar Button Item2
     - Bar Button Item3

MyViewController is used throughout my app. Sometimes it's in a UINavigationcontroller, other times it's in a UIPopoverView.

When it appears in UINavigationController, the navigation bar covers up the UIToolbar and all other content near the top of View. In this screenshot you can see that the UIToolbar is completely covered up, and UIButton w/ an image of a green light bulb is partially covered.

According to the apple documentation

Any view that needs to be anchored to the top and just below the status bar (i.e. UIToolbar, UIButton, etc.) requires additional work for proper placement.

It proceeds to give a solution that simply uses constraints to move your VC's content down by x pixels, in my case the UIToolbar. This doesn't seem like a good solution because it assumes you always want the content moved down below the nav bar. In my case, I obviously don't since MyViewController is not always in a UINavigationController. When I use the constraint solution provided in these docs, the UIToolbar is oddly floating down x pixels unanchored from the top in all cases where MyViewController is not in a UINavigationController.

Am I missing something with how I am supposed to display a VC's content within a UINavigationController? Thanks so much.

Haakon answered 27/12, 2013 at 23:19 Comment(3)
could you give some codeFewer
@Fewer I added the doc outline details and a screenshot, but there isn't really any code to show here other than something like [navcntrl push:MyViewController]. It's all work in interface builder. Please let me know if I can provide further information that would be useful. Thanks,Haakon
What iOS version are you running on?Urbina
M
23

In iOS7, UINavigationBar has translucent property and views of child VCs of UINavigationViewController are underneath UINavigationBar by default.

If you don't need this translucent effect, turn off this property by using the following code.

self.navigationController.navigationBar.translucent = NO

You can also use @ldindu's way as well.

Merrygoround answered 28/12, 2013 at 2:10 Comment(1)
This renders that bar completely transparent. If you dont want that you may then need to adjust its backgroundcolor property to change it to whatever color you need (including partial opacity)Hulbert
U
8

If you are running on iOS 7.0 version then you need to set following property which is newly introduced in iOS 7.0 as follows

self.edgesForExtendedLayout = UIRectEdgeNone; 

as by default edgesForExtendedLayout property is set to UIRectEdgeAll that means the view controllers use full-screen layout by default.

Urbina answered 28/12, 2013 at 0:42 Comment(1)
This is the best answer, imhoTremulant
F
0

Identify whether you are using iOS 7.

#define IS_IOS7 [[[UIDevice currentDevice] systemVersion] hasPrefix:@"7"];

- (float)topPadding
{
    return (IS_IOS7) ? 20.0f : 0.0f;
}

Use increment the Y axis of the frame by [self topPadding] to move it further down.

I hope your problem is solved.

Fewer answered 27/12, 2013 at 23:58 Comment(0)
C
0

If written in Swift, it's edgesForExtendedLayout = UIRectEdge()

Corunna answered 15/11, 2016 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.