1
votes

I'm still new to iOS Dev.

Goal: Create an iOS App with Navigation Bar (with .add as right bar button) at the top and Tab Bar at the bottom screen using Programmatic approach (not using storyboards and xib)

So I did almost everything here: https://developer.apple.com/library/archive/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/CombiningViewControllers.html

But apparently it doesn't work (maybe because it is old? idk) and I'm also not comfortable configuring the App Delegate yet.

So what I have are these:

  • CompanyViewController as UIViewController
  • AssessmentViewController as UIViewController
  • TabViewController as UITabBarController, UITabBarControllerDelegate

    1. I tried putting navigation controllers inside each VCs (navigationBar when I tap Tab Bar Items, which is expected - but the Title and Right Bar Button is NOT showing
    2. I tried creating Swift file UINavigationController and named it NavigationViewController then added it to the TabViewController -> viewControllers but what happened was it was added to the tab bars at the bottom of the screen so it's not what I need and it looks like an ordinary tab not a navigation bar.
    3. This is the last one I tried which displays Navigation Controller with its title but not its right bar button...

    class TabViewController: UITabBarController, UITabBarControllerDelegate { override func viewDidLoad() { super.viewDidLoad() self.delegate = self }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        let tabOne = CompanyViewController()
        let tabOneBarItem = UITabBarItem(title: "Company", image: .none, tag: 1)
    
        tabOne.tabBarItem = tabOneBarItem
    
        let tabTwo = AssessmentViewController()
        let tabTwoBarItem2 = UITabBarItem(title: "Assessment", image: .none, tag: 2)
    
        tabTwo.tabBarItem = tabTwoBarItem2
    
        self.viewControllers = [tabOne, tabTwo]
        setUpNavigation()
    }
    
    func setUpNavigation() {
        navigationItem.title = "Company Assessmentz"
        self.navigationController?.navigationBar.barTintColor = colorLiteral
        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:colorLiteral]
        self.navigationController?.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(selectorX))
    }
    
    @objc func selectorX() { }}
    
1

1 Answers

4
votes

replaced this:

self.navigationController?.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(selectorX))

to this:

navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .camera, target: self, action: #selector(selectorX))