skip to Main Content

I have three view controllers under my tabbar controller.
They extend BaseViewController for navigation customization.
In other viewcontrollers outside the tabbar controller, BaseViewController works without any problems. But inside the viewcontrollers of tabbar controller, any changes are reflected, and just shows the default navbar with default back button.

I

    func configureSettingBtn() {
        let settingImage = UIImage(named: "settingIcon")
        let settingBtn = UIButton()
        settingBtn.setImage(settingImage, for: .normal)
        settingBtn.addTarget(self, action: #selector(settingApp), for: .touchUpInside)
        let setting = UIBarButtonItem(customView: settingBtn)
        
        navigationItem.rightBarButtonItems = [setting]
    }
    
    @objc func settingApp(sender: UIBarButtonItem) {
        
    }
    
    func configureInfoBtn() {
        let infoImage = UIImage(named: "infoIcon")
        let infoBtn = UIButton()
        infoBtn.setImage(infoImage, for: .normal)
        infoBtn.addTarget(self, action: #selector(infoAction), for: .touchUpInside)
        let info = UIBarButtonItem(customView: infoBtn)
        let spacer = createSpacer()
        
        self.navigationItem.rightBarButtonItems?.append(contentsOf: [spacer, info])
    }
    
    @objc func infoAction() {
        
    }
    
    func configureAnnounceBtn() {
        let announceImage = UIImage(named: "messageIcon")
        let announceBtn = UIButton()
        announceBtn.setImage(announceImage, for: .normal)
        announceBtn.addTarget(self, action: #selector(announceAction), for: .touchUpInside)
        let announce = UIBarButtonItem(customView: announceBtn)
        let spacer = createSpacer()
        
        self.navigationItem.rightBarButtonItems?.append(contentsOf: [spacer, announce])
    }
    
    @objc func announceAction() {
        
    }

I wrote these functions in the BaseViewController and tried to use them, but it doesn’t work in the viewcontrollers under the tabbar. I tried the codes that I wrote, and also tried customizing the navbar in the storyboard but failed. I prefer the code work.

How can I solve this problem? Any ideas would be appreciated.

Ideas for adding different functionality to the button in each viewcontroller within the tabbar controller would be appreciated too.

2

Answers


  1. You can read this passage and then you will get the answer.custom navigation bar in iOS

    Login or Signup to reply.
  2. The issue you’re experiencing could be due to one of many things, so let’s see if you can try to find it like this:

    1. Make sure that your child view controllers are actually inheriting from : BaseViewController.
    2. Make sure that these functions configureSettingsBtn() and configureInfoBtn() actually get called at some point during the lifecycle of your child controllers. For example, during viewDidLoad of the child controllers.

    Try debugging and go line by line with breakpoints to see what is happening at each line and see if you can determine where it all went wrong.

    I can also suggest another approach, you could create a class CustomNavigationController like this:

    class CustomNavigationController: UINavigationController, UINavigationControllerDelegate {
        override func viewDidLoad() {
            super.viewDidLoad()
            self.delegate = self
        }
    
        func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
            if let viewController = viewController as? BaseViewController {
                viewController.configureSettingBtn()
                viewController.configureInfoBtn()
                viewController.configureAnnounceBtn()
            }
        }
    }
    
    

    And also if you want each child controller to behave differently when the buttons are pressed make sure that you override the functions like this:

    class ChildViewController: BaseViewController {
        override func configureSettingBtn() {
            super.configureSettingBtn()
            // Customize the button further if needed
            // Or set a different target action specific to this view controller
        }
        
        @objc override func settingApp(sender: UIBarButtonItem) {
            // Implement custom action for this view controller
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search