0

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 enter image description here

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
jerfin
  • 803
  • 1
  • 13
  • 28
  • embed your uitabbarcontroller to navigation controller – Anbu.Karthik May 07 '19 at 04:57
  • see this for e.g : https://stackoverflow.com/questions/29458948/how-to-use-navigation-controller-inside-of-uitabbarcontroller-with-storyboard-on – Anbu.Karthik May 07 '19 at 04:58
  • @Anbu.Karthik can i know how to do that ? – jerfin May 07 '19 at 04:58
  • @Anbu.Karthik I read the content in link, what i want is i need the login screen not to be embeded in tabbarcontroller and it should be the initial controller. the detail screen should be only be integrated in tabbarcontroller. So how to achieve this. My project flow is first its a login screen and it will redirect to some tab screens like detail screnn, how to acheive this? – jerfin May 07 '19 at 05:08
  • Check this url: https://stackoverflow.com/a/55951876/3122406 – Bhavik Modi May 07 '19 at 05:17

2 Answers2

1

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
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
  • its working good, but its seems like a tool bar at a height of 40 is attached below the navigation controller when I embeded the detail screen seperately in each navigation controller. – jerfin May 07 '19 at 10:30
  • @Arun Other tabs of the tabbar controller will be visible in the bottom of the view controller – RajeshKumar R May 07 '19 at 10:47
  • I am getting the same UI as you are telling , but I am getting a space below the navigation controller.Please run mysample project I have attached. https://drive.google.com/open?id=1KkrhF7H8lDxCw0bhR_f23Fyf_D0RdYmp . I have added a view in presentController but a gap visible between nagivation controller and the view which i have added. – jerfin May 07 '19 at 10:57
0

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
}
Faruk
  • 2,269
  • 31
  • 42