Closed Sophie1214 closed 6 years ago
@Sophie1214 Hi, can you please describe what are you trying to achieve visually speaking?
Okay, so I created an empty UIViewController
in storyboard, which is the 'parent' below, and it is embedded inside a navigation controller.
let tabController = AZTabBarController.insert(into: parent, withTabIconNames: icons)
So now the tabController is created with a navigation bar.
I want to create a Save
button at the top right corner of the navigation bar, when the first tab is selected OR when any of the tab is selected.
Do you mind sharing the entire swift class?
Sure, but it might be a little tedious.
Insert the AZTabBarController into the navigation stack:
if let navigationController = navigationController {
var viewControllers = navigationController.viewControllers
let storyBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
let parentController = storyBoard.instantiateViewController(withIdentifier: "TabRootController")
Util.createTabController(storyBoard: storyBoard, parent: parentController, asset: self.asset)
viewControllers.append(parentController)
navigationController.setViewControllers(viewControllers, animated: true)
}
Shortened 'createTabController' method:
static func createTabController(storyBoard: UIStoryboard, parent: UIViewController, asset: Asset?){
...
let tabController = AZTabBarController.insert(into: parent, withTabIconNames: icons)
let dashboard = storyBoard.instantiateViewController(withIdentifier: "dashboard") as! DashboardViewController
...
tabController.setViewController(dashboard, atIndex: 0)
tabController.setViewController(status, atIndex: 1)
tabController.setViewController(logs, atIndex: 2)
tabController.setViewController(profile, atIndex: 3)
...
tabController.highlightButton(atIndex: 0)
tabController.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save", style: .plain, target: self, action:nil)
}
Dashboard View Controller:
class DashboardViewController: UIViewController{
...
override func viewDidLoad() {
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save", style: .plain, target: self, action: #selector(save))
}
...
@Sophie1214
So if I understood your code correctly, You are trying to insert the Tab Bar into a controller which is loaded from a storyboard, then setting that controller as the root controller of the existing navigation controller. You then proceed into loading another controller from storyboard and setting it as one of the tabs of the tab bar.
Alright I think I understand the issue.
Try setting the navigation item of the tab bar instead of the of the child view controller that is set in the tab bar.
for example:
class DashboardViewController: UIViewController{
...
override func viewDidLoad() {
let tabBar = self.currentTabBar
tabBar?.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save", style: .plain, target: self, action: #selector(save))
}
...
Let me know if that solves the issue.
Unfortunately it didn't solve the issue, but you understood the setup perfectly~
Alright try one last thing:
try instead of tabBar?
tabBar?.parentViewController?
Yes it worked !! Thank you<3
I tried to create a
UIBarButtonItem
by usingBut the item failed to be created, and I tried with both the
AZTabBarController
and one of the tab controllers.A little help would be much appreciated~