iOS: Removing UINavigationBar animation - ios

Our app has an UINavigationBar with an image on it. When we segue (push) to another screen then click the back button the image on the Navigation Bar seems to animate from left to right as it reappears. This is a little distracting. How can you remove this back button animation?
We tried changing the segue Animates setting but this changes both the push animation and not the back animation.
Our Nav Bar code:
let logoImage:UIImage = UIImage(named: "ABC")!
viewController.navigationItem.titleView = UIImageView(image: logoImage)

Figured this out in large part due to this answer https://stackoverflow.com/a/8602982/47281
Create a custom Nav Bar and override popItem:
class MyNavigationBar: UINavigationBar {
override func popItem(animated: Bool) -> UINavigationItem? {
return super.popItem(animated: false)
}
}
Entered MyNavigationBar as the Navigation Bar class for our Navigation Controller via the Storyboard:
Note I did not override NavigationController popViewControllerAnimated as in the linked answer.

You can do this:
override func viewDidLoad() {
super.viewDidLoad()
let logoImage: UIImage = UIImage(named: "ABC")!
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: logoImage, style: .plain, target: self, action: #selector(backBtnPressed))
}
And then create a method to handle the tap on the button
func backBtnPressed(){
_ = self.navigationController?.popViewController(animated: false)
}

Related

Unable to add barbuttonitem to navigation controller

I would like to integrate both navigation controller and tab bar controller in my project.But I am unable to add right barbutton to the navigation controller.
I have attached the screenshot of the storyboard
What I have done is I have added navigation controller to login screen and this time I am able to add barbuttonitem both by adding code as well as by dragging barbuttonitem to navigation controller.
let addBtn = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
self.navigationItem.rightBarButtonItem = addBtn
Problem I am facing is after adding Tab bar controller I am unable to add rightbarbutton both by code as well as by dragging to the navigation controller. please help me.
When a ViewController is embedded in a NavigationController you use
self.navigationItem.rightBarButtonItem = addBtn
In your project Detail Screen isn't embedded in NavigationController directly. Detail Screen is embedded in TabBarController, TabBarController is embedded in NavigationController. So you should use
self.tabBarController?.navigationItem.rightBarButtonItem = addBtn
But this addBtn will be visible in all view controllers which are embedded in the TabBarController.
If you want to add the rightBarButton for only one viewcontroller, then embed the Detail Screen in a new NavigationController. Then you can add rightBarButton using
self.navigationItem.rightBarButtonItem = addBtn
You should be sure parent returns the top child controller of UINavigationController. In my case
parent?.parent?.navigationItem.right...
did the trick.
If you reuse the controller -as embedded or not- which you want add items to navigationItem, following example will work. However some logical changes may be needed.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard parent is UINavigationController else {
parent?.parent?.navigationItem.rightBarButtonItem = UIBarButtonItem()
return
}
navigationItem.rightBarButtonItem = UIBarButtonItem()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
guard parent is UINavigationController else {
parent?.parent?.navigationItem.rightBarButtonItem = nil
return
}
navigationItem.rightBarButtonItem = nil
}

Swift how to preserve navigation bar button items after push

In my root view controller I set an array of bar button items with images and assign them to the right bar button.
When I push the next view controller my navigation bar resets and only displays a back button.
Any way to preserve the navigation bar as it was set on the root view controller so it will display on all pages?
As user1046037 has said you can set the item buttons while you are preparing the segue.
Example:
let helpViewController = HelpViewController(nibName: "HelpViewController", bundle: nil)
let someLeftButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.refresh, target: self, action: "someAction")
helpViewController.navigationItem.leftBarButtonItem = someLeftButton
helpViewController.navigationItem.leftItemsSupplementBackButton = true
navigationController?.pushViewController(helpViewController, animated: true)
This one is to preserve the left button item and the back one.
helpViewController.navigationItem.leftItemsSupplementBackButton = true
If you are going to use the same button in several Viewcontrollers you can create a BaseViewController setting up the button and his behaviors.
class AHBaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureNavigationBar()
// Configure Common status bar if you want too.
}
func configureNavigationBar() {
let someLeftButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.refresh, target: self, action: "someAction")
navigationItem.leftBarButtonItem = someLeftButton
navigationItem.leftItemsSupplementBackButton = true
}
}
Then just inherit it, in the viewControllers that you want to show the button(s).
class HelpViewController: AHBaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
When you segue between view controllers using Push, you're going to get the default "horizontal slide" navigation animation which. Try "present Modally" or "present as popover".

Remove back button text from inherited navigation bar Swift 3

I want to know if its possible to remove the navigation bar back button text from an inherited navigation bar. Presently my navigation bar shows "< ControllerName". I want to simply show the "<" back icon. I would also like to know how to show "< Back", and how to remove it completely.
I understand I could do this by adding a bar button item to the storyboard, however is there an easier way to do it?
Note, this code does not work:
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
You better custom back button for this task.
but You also can do it in other ways. Ex: You have ViewController1, and ViewController2 (You push ViewController2 from ViewController1)
ViewController1
public class ViewController1: UIViewController {
override public func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.title = "viewcontroller1 title"
}
}
ViewController2
class ViewController2: UIViewController {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// get previous view controller and set title to "" or any things You want
if let viewControllers = self.navigationController?.viewControllers {
let previousVC: UIViewController? = viewControllers.count >= 2 ? viewControllers[viewControllers.count - 2] : nil; // get previous view
previousVC?.title = "" // or previousVC?.title = "Back"
}
}
}
I think this will work for you.
self.navigationItem.hidesBackButton = true
Solution suggested by #Prashant will remove the back button from navigation bar.
To remove the title, use following:
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

how to have the back navigation bar without the navigatin bar

I have a main page, which has a button that will go to another page.
I want when i go the second page, to have a back button at the top left of the navigation bar.
I added a navigation controller to the main view controller, and then i added a push segue to my second view controller (second page), so an automatic back button created.
what i want is that i do NOT want the navigation bar to be in the main view controller, and I don't want it to be in the second view controller, I just want to have the back button in the second view controller.
I thought about it and i came up doing:
self.navigationController?.setNavigationBarHidden(true, animated: false)
in the main view controller, that actually hide the navigation, but that makes the second view ctonroller to loose its back button.
do you have any solution to my problem?
this is the main view controller (which has the navigation bar, but i would like to not have it)
this is the second view controller, which has the back button,
I have no problem if i leave the navigatino bar, if it was transparent, any idea please? (and by transparent, i mean i can see my image bellow it)
Update 1
after the first comment gives me a hint, i tried to applied it like this:
class CustomNavigationBar: UINavigationBar {
override func drawRect(rect: CGRect) {
super.drawRect(rect)
}
}
and I set the class of my navigation bar in the UINavigationControlelr to my custom navigation bar.
and in my main view controlelr i add this:
self.navigationController?.navigationBar.translucent = true;
self.navigationController?.navigationBar.backgroundColor = UIColor.clearColor()
but the result is that my Main view controller, still has a place (though it is empty) for the navigation bar. can't i make this place as transparent to see the image bellow it?
add this to the main view controller
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: true)
}

Swift Changing Navigation Bar in a ViewController

So here's my problem.
I added a empty ViewController to a NavigationController.
The problem is that when I add a View to the empty ViewController the navigation bar becomes unclickable I can't add back buttons or change the title.
I'm using Xcode 6.1 6A1052d.
You don't add buttons directly to navigation, but to the navigationItem of contained view controller. For example,
class ContainedViewController : UIViewController {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: nil, action: nil)
}
}

Resources