move navigationBar's left and right barButtonItem's vertically upwards - ios

I am trying to move my navigation bar's left and right barButtonItem's upwards vertically without any success. I can manage to move the title view up but using the same method for the barButtonItem's doesn't do anything.
The effect I'm trying to achieve is what is used on the Bookmarks page of safari:
I'm hoping to achieve this without any subclassing of the navigationBar so that I can maintain the translucency.
My approach was to add UIView's to the navigationBar directly in IB for the leftBarButtonItem, Title and rightBarButtonItem. This works fine for the title and I'm able to add the Title and segmentedControl. The problem is when I add the left and right barButtonItem's I am not able to move them vertically above my segmented control. I am using the following code to try to move the barButtonItem's vertically in viewDidLoad():
self.navigationItem.rightBarButtonItem?.setBackgroundVerticalPositionAdjustment(-15, for: UIBarMetrics.defaultPrompt)
self.navigationItem.rightBarButtonItem?.setBackgroundVerticalPositionAdjustment(-15, for: UIBarMetrics.compactPrompt)

Related

Adding Button right of the UiSearchController swift, UIKit

I want to add an button right to the search controller in the navigation, but am not sure how can I implement that... any Ideas?
Am doing it programmatically
I want to replicate this
Use a Horizontal StackView and embed a search bar on the left, and a button on the right. Set the button's imageView to the "gear" image or whatever image you'd like.

How to make multiple right bar button items stack vertically in navigation bar?

How can I add a UIlabel to the area in green? (it will be within the navigation bar)
I was able to add the logo as a button image in rightBarButtonItem, but I can't google a way to add a label to the nav bar in this specific position.
After reading around I'm thinking maybe I have to create a custom view that would contain the logo and a UILabel, arrange them vertically within that view, and then place that view in rightBarButtonItem?
edit: This is a unique problem from other answers here in that I am looking for how to add a UILabel to an area of the NavigationBar that is outside of the "rightBarButtonItem" area. Using rightBarButtonItem or setting a custom view to appear within rightBarButtonItem does not achieve what I am looking for. I want a UILabel to appear in the NavBar on the right side aligned vertically with the "Today" title.
I ended up figuring out that using a custom header within the collectionView is the way to go (rather than trying to place a UILabel into the navigationBar via a custom UIView into the rightBarButtonItem space). This worked for me because I wanted the title and date to disappear upon scrolling anyway. Basically this mimics the "Today" tab in the iOS App Store, and my solution finally achieved what I was looking for.
Finished result:
The 2 posts that helped me set up the collectionView header and attach the labels are here:
How to add UICollectionView Header
Display Section Header UICollectionReusableView
You can follow this code snippet:
let containerView = UIView()
...
let rightBarButton = UIBarButtonItem(customView: containerView)
navigationItem.rightBarButtonItem = rightBarButton

Ios Swift : Adding or Moving NavigationBar to bottom of the view controller

I want to move the navigation controller bar to the bottom of the view controller. How can i get this done ?
I tried :
self.navigationController!.navigationBar.frame = CGRectMake(
0,
UIScreen.mainScreen().bounds.height - 50,
UIScreen.mainScreen().bounds.width,
50)
This is moving to the bottom but hiding all other controller objects and also back button is not woking.
Sujay U N,
You should not try to move the UINavigationBar provided by the embeded UINavigationController to the bottom of the screen. Trying that will obvisoulsy move all the view's below it causing all the controller objects to hide.
Workaround
Approach 1:
Consider using ToolBar :)
Toolbar is designed to be placed at the bottom of the screen. If you are using xib or storyboard you can pick toolbar from components library and place it on your ViewController's bottom and then apply autoresizing masks or constraints properly :)
Now in order to show the back button make use of UIBarButtonItems. Change the style to custom and provide it arrow image or provide default style as done.
Though now you are all set to go :) You will notice UINavigationBar at the top of your view controller. In order to get rid of it,
select your ViewController, select its TopBar property set it to none :)
Approach 2
Use UINavigationBar.
Specific about using Navigation bar and dont want to use toolbar, well you can do the same thing with UINavigationBar as well.
Drag the UINavigationBar from components library place it at the bottom of the screen. Drag the UIBarButtonItem drop it as leftBarButtonItem, change the barButtonItem image to your back image. ( Same process as UIToolBar just use UINavigationBar instead)
Understand this is not same as the navigation bar provided by the embeded NavigationController. So get rid of NavigationBar at the top of your ViewController same as I explained above here as well
Finally,
In both the cases, draw an IBoutlet from barbutton item and handle poping the viewController programmatically.
Happy coding :)

iOS/Swift: Dynamically Resize a Toolbar

I'm trying to make an app with a toolbar that can be resized. Basically, the toolbar can alternate between being at the bottom of the view and being at the top. When a button is pressed, it switches from one to the other. The problem is that when it is at the top, I want the size of the toolbar to expand to accommodate the status bar, but I don't know how to do this.
I've seen some solutions for changing the toolbar size but they all seem static and not something that can be changed with the tap of a button. Any suggestions on how to do this? Perhaps a different solution altogether?
You can use a normal UIView and customize it so it looks like a UIToolbar, then just set constraints using AutoLayout and animate the height-constraint.

Recreate UITabBar to UIToolbar on select from Photos app

I need to do exactly what the photos app is doing when you press the select button. Basically just hides the UITabBar and presents a UIToolbar. For some reason this seems to be incredibly difficult if you don't want to implement a complete hack. I found a hack if you shrink the height of the UITabBar and change it's alpha to 0 but when you set it's height back to the default the image and text is condensed.
Turns out that I ended up just needing to call [self.tabBarController.tabBar setHidden:YES] to hide the tabBar and instead of using my existing UINavigationController's toolbar I create my own instance of a UIToolbar and add it as a subview of my view controller. Then using autolayout I pinned it to the leading, trailing, and bottom edge of the view. This handles rotation and other issues.

Resources