How to position the NavigationBar x pixels from the top - ios

I want to position the UINavigationBar x pixels from the top. Is this possible? Here is a screenshot of the desired result.
To clarify, I want the NavigationBar to be positioned in the orange area. The area black area above is an ARKit view running and should not be covered by anything.
Is this even possible?
I've tried setting the NavigationBar frame and messing around with the positionForBar property. Nothing have an effect, the NavigationBar is always rendered at the top. One thing that add is that this solution, if there is any, has to be done in code and not in storyboard.

Just create a constraint from the Top Safe Area Guide's bottom to your view...?

Unfortunately, You have said it, you changed the frame but nothing happened as you don't have the ability to change(Move or resize) the UINavigationController's navigation bar. To solve this problem you have to write your own view controller class.

Try setting the navigation bar’s transform.
i.e.
navigationController?.navigationBar.transform = CGAffineTransform(translationX: 0, y: yourTopInset)

Related

Is there a way for a logo image to have a greater height than the navigation bar and still be fully visible?

I have a situation where I need to create something like this:
The problem is that the logo is higher than the navigation bar is, and I want this all to be a part of the navigation bar. I’ve tried “pushing” the logo from the top, but it doesn’t really work. If I increase the height of the logo image, it just cuts it off where the navigation bar ends.
Also, the back button should be aligned to the top of the logo image.
Currently I am using a custom view for this and hiding the navigation bar, which I hate, so if anyone has any advice/suggestions it would be greatly appreciated!
Thanks!
Try setting title view's clip to bounds and masks to bounds to false by setting
self.navigationItem.titleView?.clipsToBounds = false
self.navigationItem.titleView?.layer.masksToBounds = false

Set navigationbar size according to background image

I have set an backgroundImage for my navigationbar. This works fine. But I would like to have the navigationbar height to be adjusted to the background image. At the moment the width of the background images is also not set according to the screen size.
I tried setting the height of the navigationbar like described here. This shows a bigger navigationbar for like a second but then it shrinks to its default size again.
Does anyone know how to achieve what I want? Here is an example of what I want to achieve: image
Apple Documentation:
It is permissible to customize the appearance of the navigation bar
using the methods and properties of the UINavigationBar class but you
must never change its frame, bounds, or alpha values or modify its
view hierarchy directly.
To achieve the effect seen in the image you tagged, they are most likely using a collection view to layout their data and that image is part of the collection view's header. They made the navigation bar background color clear, but the image is definitely not part of the navigation bar itself.
Apple recommends to never change the frame of a navigation bar manually because it messes with the layout code of its subviews and animation methods.
You could either subclass the navigation bar and attempt to create something similar, or go the easier route and make the navigation bar clear (UIColor(white: 0, alpha: 1) not .clear otherwise it might show up incorrectly) and have an underlying view display the image (e.x. a collection view whos header extends to the top of the view controller).
This will allow you to adjust the image height and width freely without subclassing the navigation bar, and creating potential bugs.
You can make custom NavigationBar class.
It can be help you
https://developer.apple.com/library/archive/samplecode/NavBar/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007418-Intro-DontLinkElementID_2

Prevent Tab Bar from cutting off WKWebView text

Our UITabBar is cutting off the bottom of our WKWebView text.
Our Storyboard view has a few labels at the top of the screen and a WKWebView set below those labels programatically. It works great but you can't scroll to the end of the WKWebView to see the last line of the text.
I believe this doesn't happen with UIWebView but that API is deprecated.
I understood automaticallyAdjustsScrollViewInsets to be the solution but apparently that doesn't work in this case.
Have tried setting Extend Edges \ Under Bottom Bars to false on the Storyboard. This comes close to solving the issue - the scrolling issue is resolved. Only issue is the Tab Bar then changes color to a darker color and I can't seem to get that to revert to the default lighter grey metallic color.
What is the correct way to do this?
WKWebView contains UIScrollView. UIScollView has contentInset property.
https://developer.apple.com/reference/uikit/uiscrollview/1619406-contentinset
Use this property to add to the scrolling area around the content. The unit of size is points. The default value is UIEdgeInsetsZero.
Figured it out based on some other SO posts:
webView.scrollView.contentInset = UIEdgeInsetsMake(0.0, 0.0, controller.tabBarController!.tabBar.frame.height, 0.0)
... maybe there is a better way (?) but this worked.

MKMapView Content Insets with Navigation Bar

I'm experiencing a strange issue with MKMapView placed in a view with a navigation bar. Given a UIScrollView in the same situation, whose width, height, and center are constrained to it's superview (the view controller's view object) the insets will be adjusted if the controller's automaticallyAdjustsScrollViewInsets property is set to YES, and not if set to NO. That's great. I can not find this documented anywhere, but the map view seems to do the same thing, but it's not possible to disable it. Setting automaticallyAdjustsScrollViewInsets has no effect.
In brief(ish):
In the image below, the small back square is centered within the view, regardless of navigation bar. The map view is also constrained to be exactly the size of the view (it extends behind the nav bar). The location of the map view was set to be the current location (also indicated by the blue circle) so I would expect them to match, but they are off by half the combined height of status and navigation bars.
Is there an explanation for this behavior, and a way to get it to work as expected?
This is very strange because an MKMapView is not a subclass of a UIScrollView but it is behaving like one in this case. That's not completely unreasonable, but I would expect the auto adjusting insets to work consistently on a map view and a scroll view.
Workaround
Something awful like this would 'solve the problem':
MKCoordinateRegion region = MKCoordinateRegionMake(self.coordinate, MKCoordinateSpanMake(0.008, 0.008));
MKMapRect mapRect = MKMapRectForCoordinateRegion(region);
[self.mapView setVisibleMapRect:mapRect edgePadding:UIEdgeInsetsMake(0, 0, 64, 0) animated:YES];
Either adjusting the bottom inset as well, or adding a negative inset on the top gets things lined up, but it would break if this behavior of the map view changes, or if the view controller is presented without a navigation bar, or if there is a stiff breeze. It also erodes my confidence in pulling correct coordinates back out of the map view.
MKMapView does indeed sit under the navigation bar, but automatically uses a housing view controller's topLayoutGuide to shift annotations and the compass view, etc. down from under it.
You should make sure that your own views take this into account in their autolayout code.
If you take a look at this, you can see that the mapview is very likely not sitting under the navigation bar, as you think. It is sitting between the bottom of the nav bar and the bottom of the superview.
On another note, the navigation bar is translucent by default, so if the mapview was really sitting under the nav bar you would be able to see through it, assuming you didn't disable the default nav bar behavior.

UIToolbarPosition set to bottom on resize

I have a UIToolBar at the top of one of my views and the shadowing shows up on the bottom of the tool bar as I'd expect. I know UIToolbarPosition is internally set to UIToolbarPositionTop.
When I rotate my device (iPad), the tool bar grows (using auto sizing in IB) and the UIToolbarPosition changes to UIToolbarPositionBottom which flips the shadow to be at the top of the bar. If I don't have the bar resize, it remains correct, but of course doesn't stretch to match screen width.
So, what gives? What would cause the UIToolbarPosition to flip on me? This is sitting at 0,0 the whole time and only changes width based on rotation.
Extra: I've considered work arounds like using a UINavigationBar. UIAppearance is probably a no go because I do have a toolbar at the bottom too and I do want my "top" different than my "bottom"
I eventually solved this using the UIBarPositioning protocol on UIToolBarand setting the barPosition = UIBarPositionTop

Resources