HI WELCOME TO KANSIRIS

How to Combine Ionic Side Menu and Tabs Navigation

Leave a Comment
The navigation inside Ionic projects is in general quite simple, but once you try to combine different navigation patterns things can get really tricky. Especially one case happens a lot in reality, which is using an Ionic Side Menu and a Tab Bar navigation together.
Inside this tutorial we will see how we can put together our different view elements in a way that makes sense and works super easily. Some of the code is inspired by the official Ionic conference app, so if you want an even bigger example you can check that repo out.
We will keep things a bit simpler, so in the end we will be able to transition from a dummy login page to our “inside” navigation which combines side menu and tabs.
ionic-side-menu-tabs

Get the Ionic Menu + Tabs Template

Enter your Email below to receive the needed files directly to your inbox!
Powered by ConvertKit

Starting our App

We start with a blank template just so we can see what pages and components are needed to put together this kind of navigation. Of course we could also start with the given Ionic templates like side menu or tabs, but I felt starting at zero helps us learn the concepts even more.
We need to create quite a few pages which we will fill with live later on, so go ahead and create everything we need right in the beginning:
You can delete the default home/ folder as he doesn’t contain a modules file and we want to use lazy loading. You could also create a file for that inside the folder, but we have generated already everything new so we’re already behind that point now ðŸ˜‰
Next we need to change the entry point of our app to be our new created LoginPage, so open your src/app/app.component.ts and change it to:
Now the app will start with the blank login page. Also if you remove the HomePage folder, make sure to remove the reference to it from the app.module.ts!

The Login Screen

Our login screen is actually just a placeholder for your login, we won’t implement anything like this now. You can check out the login example for more ideas how to build your login!
We only need a button so we can move on from that screen, so open your src/pages/login/login.html and change it to:
The function inside the class will now change our navigation by calling setRoot() which basically resets the navigation instead of pushing a new page onto the stack. Otherwise we would have a back arrow to the login page which we don’t really want.
Go ahead and add this to your src/pages/login/login.ts:
So now after we hit login, we will be on the template for our menu. But there’s no menu yet, so let’s change that.

Building the Menu Navigation

The menu pattern is always the same, we need to define the menu area and also the content area inside a view. In our case we construct the menu items from an array of pages which we will create in the next step.
Each button will automatically close the menu using menuToggle and we also dynamically set the icon and color of the button. Setting the color helps to reflect the changes of the tab bar also inside the menu!
Go ahead and add this menu code to your src/pages/menu/menu.html:
Now we get to the actually most difficult part of this tutorial, because once we select a menu item we need to change our content view and also make sure our tab bar also marks the correct item.
As said before we will keep an array of pages, and those are of the type PageInterface which we defined above the class.
You will see why each element needs the attributes of that interface once we go through the functions.
Also, we grab a reference to the root navigation by using @ViewChild so we can directly work on that element!
Inside the array we keep 3 pages; two of them will be also tabs and 1 is just a special page which should have no tabs visible.
The important part here are now the 2 functions that are called once we open a page from the side menu and the one to check if the current element should be marked as active. For now add the code to your src/pages/menu/menu.ts:
First of all when we want to transition to a page, we grab the optional index for that page. Also, we try to get the active child nav, which would mean a reference to the tabs navigation.
If we have both of these information, we can simply use the select() function of the tab bar to select the tab with the according index. Of course the index in our array should reflect the position of the element in the tab bar (we will create the tab bar in the next step).
When the check fails, we more or less reset our root navigation of the content to the new page and pass any additional params to it. This will be the case when we select our special page which has no tab bar, or when we navigate back from that page because then also no tab bar is active (at the moment of the transition).
Any questions so far? Just let me know below, I know this is not super easy.
To mark the active element we again try to grab a reference of the tab bar and check if the selected root is equal to the component specified for the page.
As a fallback for our special page we also make a check on the root navigation and see if the names match, by doing this we can also mark our special page as active correctly!
That was the hard part of this tutorial. If you are still with me, things will get easier from here. Take a moment to go through these 2 functions, initially it also took me a moment (or 2, 3…) to completely understand what’s going on here.

Adding the Tab bar

As we approach the end, we haven’t actually added our tabs, so change this by opening your src/pages/tabs/tabs.html and change it to:
A very classic tab bar implementation with 2 tabs. The only really important thing here is that we are able to dynamically change the selectedIndex of our tab bar!
We will make use of this inside the constructor of this class. If you remember, we create new pages and also pass the params object through, so in case our tab bar is created newly we can access the tabIndex which was passed through from the menu.ts!
By doing this our tabs will be always initialised back with the correct tab to be selected. Go ahead and change your src/pages/tabs/tabs.ts to:
Now we only need to add a menu button to all of our pages so we can toggle the menu from everywhere. Go through these 3 files and change all of them like in the example below, perhaps change the content and title so you can actually see that a different page is displayed!
Files to change:
  • src/pages/tab1/tab1.html
  • src/pages/tab2/tab2.html
  • src/pages/special/special.html
The general HTML template for these files:
Now all you need to do is run the app and enjoy your full side menu with tabs navigation!

Conclusion

It’s a bit tricky to get all the features working when combining different navigation patterns. But the important thing here is it is definitely possible, and the result is exactly what the behaviour should look like!
Leave a comment below if you enjoyed this a bit different tutorial from what I normally do on some of the more “core” aspects of Ionic!

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.