Swift ios odd space when hiding navigation bar - ios

When I hide my navigation bar I end up with some space under it, it looks like the status bar is twice as big as it should be.
In the image the red line is just touching where the statusbar/navigation bar ends.
My VC is contains a scroll view that is spaced 0 to the edges and spaced 0 to the top layout guide.
Why is the navigation bar not hiding completly? I thought that if you hide the navigation bar you would end up with a 20px statusbar showing only.
I could space my scrollview to be +20 from the top view but that seems a bit hacky.

Look for Extended Edges in attribute inspector of the view controller and uncheck Under Top Bars.

Related

Don't Extend Edges Under Top Bar, but with white/translucent navbar?

I had weird extra space between the navbar and the top cell of my table view, so I searched around and disabling "Extend Edges > Under Top Bars" got rid of the space, but now my nav bar is grey and boring. "Adjust Scroll View Insets" has no effect either way, btw.
Is there a way to have a normal-looking nav bar without the extra space??
Here's how it looks with the extra space
Here's how it looks with "Extend Edges > Under Top Bars" turned off:

navigationbar items not centered vertically

I am trying to place all elements of a navigationBar in the vertical center, but I don't know how to do it?
The gray part above the yellow is the navigationBar. As we can see the title and other elements of the navigationBar are not vertically centered.
How can I do that ?
My Code so far :
let navigationItem = UINavigationItem()
navigationItem.title = sTitle;
navigationBar.frame = CGRectMake(0,0,screenwidth, 50);
navigationBar.titleTextAttributes = [ NSFontAttributeName: titleFont, NSForegroundColorAttributeName: UIColor.blackColor()]
self.view.addSubview(navigationBar);
You can center all your elements ,if you hide the status bar,which can be achieved by add the following to your Info.plist:
<key>UIStatusBarHidden</key>
<true/>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
And your ViewController would look like this:
You can see more ways to hide the status bar in this link :hide Status bar
Firstly try to use native approach to set navigation bar title like:
self.title="Test title"
Or if you would like to have custom font try this :
self.navigationController.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "CustomFont", size: 15)!]
As is mentioned in the comments, the reason these elements are not vertically centered is the fact that the status bar is shown, which reserves 10 points at the top of the navigation bar. It's just not easily visible because it's using the light content style. If your battery was low or charging you'd see the battery at the top right.
You'll need to remove the status bar which will decrease the height of your navigation bar. As there are different ways to hide the status bar based on project setup and context I won't instruct how to remove the status bar, but information on how to do so is widely available.
Once that extra space is removed your items will be vertically centered.
The navbar text/items will never look centered, the way you are displaying things. Technically they are already centered in the Nav bar, but the nav bar manages the centering, and keeps all elements constrained to the lower edge of the nav bar.
The status bar needs 20px. The nav bar should be 44px in height. The top of the nav bar should be constrained to the Top Layout Margin, NOT 0 to the top view boundary itself. The reason for this is that ios removes the status bar (actually the top 20px of whatever displays in portrait mode) when the device is flipped to landscape mode. If your nav bar is 44px, switching the device to landscape mode will show you that the text is correctly centered vertically.
My suggestion for you to see all of this is to create a small view above the nav bar. Make the view 20px high, constrain it to the View.Top (NOT to the margin), and set the background or tintcolor to bright green or something. Then set your nav bar to 44px, constrain it to the Top Layout Margin Guide (NOT to the bottom of the status view you just created, and NOT offset from the View.Top Boundary).
Then when you flip the device back and forth, you'll see that your text is always centered within the nav bar, and the status bar will come and go above the nav bar.
PS. Do this in the storyboard first, it's easiest that way.

UIView Under Nav and Tab Bars

Using storyboard and Swift, I have a view controller which has two elements on it. A UITextField, and a UIView, which I use merely to give a colored border to the UITextField. The UITextField is a child of the UIView. See:
The problem I am having is that despite having set the top bar and bottom bar simulated metrics attributes to Opaque Navigation Bar and Opaque Tab Bar respectively, See:
When the app builds and runs the top of the UIView is always underneath the nav bar. My over all feeling is that this is somehow a constraints issue but I have not been able to find the solution to it. How do I set the constraints so that the UIView is always immediately underneath the nav bar, and the bottom of the view is always just on top of the tab bar?
Select your UIView in storyboard and assign constraints to the top margins and to bottom margins. There is a 'Pin' button you use to do this that looks something like a Tie-Fighter ship in Star Wars, it is located at the bottom right of your storyboard view.
This will set margins to stretch to top and bottom always.
That is what the layout guides are for. Pin the top to the Top Layout Guide and the bottom to the Bottom Layout Guide. The guides will always move to adjust for any top and bottom bars.

How to give automatically top space to navigation bar in ios?

I have a UIViewController. I am designing the screen for iPhone 5s. I have a UIImageView & I am giving top space to container of 20. I also have UINavigationBar at top.So the space given of 20 is not enough. UIImageView is hidden behind the UINavigationBar. I can resolve this issue by giving much space from top. But in that case at design time my UI will not look proper. So is there any way by which space from UINavigationBar is automatically calculated?
In Attribute Inspector set Top Bar to Translucent Navigation Bar like this
then set Top Space of imageView to Top Layout Guide like,
set navigation bar type as opaque Navigation Bar so it will opaque that much space from your view controller and show your view content after navigation bar space they will not hide inside of navigation bar.
but if you set navigation bar type as Translucent Navigation bar then your content will hide inside of navigation bar so set "Opaque Navigation Bar" type. no need to do any other stuff for that.
I hope it will help you!

UIView bringSubviewToFront Breaks AutoLayout for UIScrollView

I have a rather simple screen where I am inserting a custom UINavigationBar at the top of the screen and a content area below it. I'm using AutoLayout and I'm visually declaring the relationship as so:
#"V:|-0-[_navBar(65)]-0-[_content]-0-|";
Now, this seems to work perfectly until I tried to have another view slide down from under the navigation bar. The nav bar contains a search field and I want the search results to slide down from under nav bar. As the code currently stands, the search results slide down on top of or in front of the nav bar and it looks quite strange. So I tried to push the nav bar to the top of the index stack using:
[self.view bringSubviewToFront:_navBar];
As far as the animation is concerned, it works perfectly. The search results no longer slide over the nav bar but they are in front of the content area. When the child of _content is a UIView it seems to be OK. However, when the child of _content is a UIScrollView, this seems to have introduced a 20px margin between the nav bar and the content area. Honestly, the space seems to be very close in size to the status bar although the nav bar has stayed the same height. I've tried bringing the nav bar to the front using many different techiniques but as soon as the nav bar and the content area are on different planes, it seems to break the autolayout constraint of 0px that is defined above.
Here is a sample project to show the bug. Look at the third item, "bringSubViewToFront bug".
Does anyone know what's going on?
The 20 point space seems like it would have something to do with the status bar, but I'm not sure why bringing the navigation bar to the front would apparently cause the scroll view to inset its content. Anyway, I was able to fix it by adding this line, after you bring the navigation bar to the front,
_scrollView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);

Resources