0
votes

i am wanting to add a barButtonItem onto a tabBar so i don't have to use a navigation controller at the top of the screen.

My ViewControllers are embedded in a tabBarController:

enter image description here

And i want to add a barButton on each viewcontroller e.g.:

enter image description here

and them implement some code like:

    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        if item.tag == 4 {

        }
    }

to run a function when this button is tapped.

problem is this button isn't showing on the tabBar when the app is built.

enter image description here

i was hoping this would achievable without having to create a custom tabBar.

2
Just to be clear, you want the log out button to show in each View Controller for each tab?Mochi
sorry the logout button is just there temporarily. i was trying to add the red menu button to the tab bar, which i have done in interface builder with a bar button item. this button does not show in the tab bar when the app is built.Pippo
You want to implement that red menu button as one of the tabs in your UITabViewController or at the top of your screen?Mochi
in the tab bar at the bottom, if i wanted it at the top id just use navigation bar. bot don't see the point of having a big bar at the top of the screen for 1 buttonPippo

2 Answers

0
votes

I don't quite understand what the problem you're describing is. Are you trying to add a 'Logout' button at the top right of your screen by only using a UITabBarController? Why are you against using a UINavigationController? It's incredibly easy to implement using storyboards (which it looks like you're using anyway).

Apple even has a page on how to implement a UINavigationController within a UITabBarController here: https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/CombiningViewControllers.html

Key information here:

To create a combined interface with three tabs that contain custom view controllers and one tab that contains a navigation controller:

  1. Create three custom view controllers, one for each tab, and a navigation controller.
  2. Select the three custom view controllers and the navigation controller (only the navigation controller scene, not it’s root view controller).
  3. Choose Editor > Embed In > Tab Bar Controller.
  4. Display the tab bar controller as the first view controller by selecting the option Is Initial View Controller in the Attributes inspector (or present the view controller in your user interface in another way.)
0
votes

According to Apple Documentation,

UIBarButtonItem - A bar button item is a button specialized for placement on a UIToolbar or UINavigationBar object. It inherits basic button behavior from its abstract superclass, UIBarItem. The UIBarButtonItem defines additional initialization methods and properties for use on toolbars and navigation bars.

Since you're against putting a Navigation Controller in your project, a UINavigationBar is not implemented which is what UIBarButtonItems are for. If you want to implement your Menu Button on top of your screen in one of your view controllers. Why not just use a UIButton as inside a UIView like this?? The blue background is just a regular UIView with UIButtons inside of it.

enter code here