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
You can read this passage and then you will get the answer.custom navigation bar in iOS
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:
: BaseViewController
.configureSettingsBtn()
andconfigureInfoBtn()
actually get called at some point during the lifecycle of your child controllers. For example, duringviewDidLoad
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: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: