I am new to swift and iOS development.
In my project i am loading slidein menu with MMDrawerController, it works fine for slide ,but after initializing MMDrawerController in AppDelegate, by top navigation bar [navigation controller] is not getting displayed. commenting MMDrawer initialization shows the navigation bar and click events are firing proper , following is the navigation initialization code ,
func buildNavigationDrawer()
{
// Instantiate Main.storyboard
let mainStoryBoard:UIStoryboard = UIStoryboard(name:"Main", bundle:nil)
// Create View Controllers
let mainPage:TabBarViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("TabBarViewController") as! TabBarViewController
let leftSideMenu:LeftSideViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("LeftSideViewController") as! LeftSideViewController
let rightSideMenu:RightSideViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("RightSideViewController") as! RightSideViewController
// Wrap into Navigation controllers
let leftSideMenuNav = UINavigationController(rootViewController:leftSideMenu)
let rightSideMenuNav = UINavigationController(rootViewController:rightSideMenu)
// Cerate MMDrawerController
drawerContainer = MMDrawerController(centerViewController: mainPage, leftDrawerViewController: leftSideMenuNav, rightDrawerViewController: rightSideMenuNav)
drawerContainer!.openDrawerGestureModeMask = MMOpenDrawerGestureMode.PanningCenterView
drawerContainer!.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.PanningCenterView
// Assign MMDrawerController to our window's root ViewController
window?.rootViewController = drawerContainer
}
In one of the examples about the MMDrawerController there is this code:
self.drawerController = [[MMDrawerController alloc]
initWithCenterViewController:navigationController....
Try to change your mainPage with a UINavigationController with the rootViewController linked to the TabBarViewController and you should be solve the issue.
DETAIL:
// Wrap into Navigation controllers
let leftSideMenuNav = UINavigationController(rootViewController:leftSideMenu)
let rightSideMenuNav = UINavigationController(rootViewController:rightSideMenu)
let centerMenuNav = UINavigationController(rootViewController: mainPage)
// Cerate MMDrawerController
drawerContainer = MMDrawerController(centerViewController: centerMenuNav, leftDrawerViewController: leftSideMenuNav, rightDrawerViewController: rightSideMenuNav)
Related
I am unable to get each tab to show up before it is clicked, I tried setting each one to its own view controller then changing the title that way but it's still not working ( sorry if my formatting is off , this is by first post on here)
thanks
#objc func didTapButton(){
// create and present tab bar controller
let tabBarVC = UITabBarController()
let vc1 = UINavigationController(rootViewController: FirstViewController())
let vc2 = UINavigationController(rootViewController: SecondViewController())
let vc3 = UINavigationController(rootViewController: ThirdViewController())
vc1.title = "My Groups"
vc2.title = "My Events"
vc3.title = "Browse Events"
tabBarVC.setViewControllers([vc1, vc2, vc3], animated: false)
guard let items = tabBarVC.tabBar.items else{
return
}
let images = ["bell", "star", "person"]
for x in 0..<items.count{
items[x].image = UIImage(systemName: images[x])
}
tabBarVC.modalPresentationStyle = .fullScreen
present(tabBarVC, animated: true)
}
Screenshot:
From the doc :
Configuring your tab bar programmatically:
To configure the tab bar associated with a UITabBarController object, configure the view controllers associated with the tab bar controller. The tab bar automatically obtains its items from the tabBarItem property of each view controller associated with the tab bar controller.
To configure tab bar items directly, use the setItems(_:animated:) method of the tab bar itself.
You can not use tabBar.items property directly as you do
I want to be able to create a TabBarViewController with some tabs and then push into the given tabs
let tabBarViewController = UITabBarController()
let redVc = UIViewController()
redVc.view.backgroundColor = .red
let blueVc = UIViewController()
blueVc.view.backgroundColor = .blue
tabBarViewController.viewControllers = [redVc, blueVc]
This created a tabBarViewController with a red and a blue tab. Now I want to push a yellow VC to the red tab so that I have a yellow and a blue tab.
let yellowVc = UIViewController()
yellowVc.view.backgroundColor = .yellow
tabBarViewController.modalPresentationStyle = .fullScreen
// this doesn't work
viewController.present(tabBarViewController, animated: true)
// must use this
tabBarViewController.viewControllers![0] = yellowVc
What should I do to be able to present in a given tab?
You can setup your UITabbarController in such a way, that each of it's child is a UINavigationController.
All together, you then have the following hierarchy:
UITabbarController
child1 (UINavigationController)
first content ViewController
child2 (UINavigationController)
second content ViewController
Now from within each contentViewController, you can use navigationcontroller.push to push a new viewController to the stack and it will stay inside the tabbar.
I added 2 ViewControllers into a TabBar, but now, the TabBar's NavigationBar 'took over' each view's NavigationBar.
I can't set a title for each one of them, I can't add buttons, nothing.
I tried a few solutions to solve it that I found on the internet, but nothing worked.
I need control over the NavigationBar of each one of the views, as I need them to be different, with different title, etc.
This is my TabBar code:
class TabBar: UITabBarController {
let homeVC = HomeVC()
let followingVC = FollowingVC()
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.appColors.mainWhite]
navigationController?.navigationBar.tintColor = UIColor.appColors.mainWhite
navigationItem.setHidesBackButton(true, animated: false)
homeVC.tabBarItem = UITabBarItem(tabBarSystemItem: .topRated, tag: 0)
followingVC.tabBarItem = UITabBarItem(tabBarSystemItem: .contacts, tag: 1)
let tabBarList = [homeVC, followingVC]
viewControllers = tabBarList
}
}
I really need the option to configure each NavigationBar from it's own ViewController, or atleast from the TabBar class.
You should add UINavigationController to each of your ViewControllers not your UITabBarController
First remove the UINavigationController of your TabBar, Either you have done this pragmatically or in the storyboard remove that first.
Second add UINavigationController to you ViewControllers
class TabBar: UITabBarController {
let homeVC = HomeVC()
let followingVC = FollowingVC()
override func viewDidLoad() {
super.viewDidLoad()
homeVC.tabBarItem = UITabBarItem(tabBarSystemItem: .topRated, tag: 0)
followingVC.tabBarItem = UITabBarItem(tabBarSystemItem: .contacts, tag: 1)
let homeNavigationController = UINavigationController(rootViewController: homeVC)
let followingNavigationController = UINavigationController(rootViewController: followingVC)
let tabBarList = [homeNavigationController, followingNavigationController]
viewControllers = tabBarList
}
}
Now if you change any properties like title and barButtons it will reflect accordingly.
Figured it out, the solution is:
The NavigationBar was not the TabBar navigation bar, but the screen that lead to the TabBar (Login Screen for example), I fixed it by hidding the navigation bar of the login screen when transfering to the TabBar controller, now the navigation bar of each view controller is shown and not blocked by the Login Viewcontrolelr navigationbar.
I have a tab bar controller and 3 viewControllers connected to it and when I move data between the viewControllers I use the following code, which works perfect:
let secondTab = tabBarController?.viewControllers![1] as! ImageViewController
secondTab.imageArray = images
Now I added navigation to the second viewController with EDITOR->Embed In....so, there is now a navigationController between my ImageViewController and the tabBarController. How can I still get the data to the ImageViewController?
You can try
if let nav = tabBarController?.viewControllers![1] as? UINavigationController {
if let let secondTab = nav.topViewController as? ImageViewController
secondTab.imageArray = images
}
}
I'm implementing a drawer layout design in an app.
But my app starts with a small screen with an animated logo (simple HTML5 animation), then a login screen (g+ and Facebook), then the main screen where I'm implementing the MMDrawerController.
The question is in my AppDelegate:
window?.rootViewController = centerContainer
window?.makeKeyAndVisible()
So the app start in this screen. Is it possible to not make rootviewcontroller the center container and still using MMDrawerController?
I need to add MMDrawerController to my third viewcontroller in my app
But, in order to MMDrawerController to work, it requires to be the rootViewController
I allready tried to add to my first ViewController an Empty MMDrawerLayout but, then, the third controller no longer works
//global var
var centerContainer : MMDrawerController?
// then the appdelegate
let rootViewController = self.window!.rootViewController
let mainStoryBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let centerViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("GaleriaPeliculas")
let leftViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("LeftSideViewController")
let leftSideNav = UINavigationController(rootViewController: leftViewController)
let centerSideNav = UINavigationController(rootViewController: centerViewController)
//And here is the problem,
window?.rootViewController = centerContainer //how can it work without this line??
window?.makeKeyAndVisible()
Is it possible to not make rootviewcontroller the center container and still using MMDrawerController?
No. You need to keep MMDrawerController as the rootViewController if you still want to make use of the left/right drawers. It's no different than a UITabBarController in the sense that it contains multiple view controllers to be conditionally displayed.