Swift Changing Navigation Bar in a ViewController - ios

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)
}
}

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".

Combine Seques with UINavigationController

I am building a mobile app which displays stats from multiple APIs.
The app shows different values based on the API that is active. At runtime, the user can select which API to display values for. However, the values are always displayed within the same Target view controller.
UINavigationController is in use and I'd like to defer to that for navigation and navigation UI, if possible, even though UITableViewControllers support navigation bars.
Please see the storyboard design image at the end of the question.
Environment: iOS 9/Swift 2.2/XCode 7
At runtime, the user will select a Choice view controller from the Select controller. What is the appropriate way to segue from one of the Choice view controllers, via a "Done" UIBarButton, to the Target controller? The Target controller does not change.
Must I programmatically define the right bar button to "Done" for every controller that is embedded within the UINavigationController?
class UIViewController: UITableViewController {
override func viewWillAppear( animated: Bool) {
super.viewWillAppear( animated)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
}
}
The proper way to return to the root view controller is:
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(MyViewController.done))
}
func done() {
if let navigationController = self.navigationController {
navigationController.popToRootViewController(animated: true)
}
}
}
You do need to programmatically add a "done" button; but it's more simple than that. Just select your root view controller, and copy/past the class in storyboard. Use your segues to push the correct view onto the UINavigation stack.

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)

Swift updating barButtonItems for a view embedded in a tabBarController in a navigationController

Basically I want to set up a barbutton item in my HomeViewController but it's not showing up. I am thinking it might have something to do with my navigation hierarchy. Here is my code for the HomeViewController:
override func viewDidLoad() {
super.viewDidLoad()
backBtn = UIBarButtonItem(title: "< Back", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("goBack"))
self.navigationItem.setLeftBarButtonItem(backBtn, animated: true)
}
Here is a screenshot of my storyboard. I dynamically push a login screen on didFinishLaunchingWithOptions when not logged in, which is why the UITabBarController is embedded inside the UINavigationController and not the other way around:
Try with following Navigation flow.
You must need to set ViewControllers of UITabbar which are embedded in UINavigationController
Please refer following image for Storyboard.

Resources