Using Ionic 4, and tabs.
Suppose that we have Tab1 / Tab2 / Tab3 with the default app generated by Ionic.
I want to use the hardware back button, to exit app if I'm in Tab1, but I need to go back on Tab1 if I am in an other tab.
Currently my code works, but :
For example if I am in Tab2 then I use the hardware back button, then the routing works, I am redirected to Tab1, but Tab2 icon remain selected. (Tab1 icon is also selected but it's normal since I have been routed to this tab)
tabs.html :
<ion-tab-bar slot="bottom">
<ion-tab-button tab="tab1">
<ion-icon name="flash"></ion-icon>
<ion-label>Tab One</ion-label>
</ion-tab-button>
<ion-tab-button tab="tab2">
<ion-icon name="apps"></ion-icon>
<ion-label>Tab Two</ion-label>
</ion-tab-button>
<ion-tab-button tab="tab3">
<ion-icon name="send"></ion-icon>
<ion-label>Tab Three</ion-label>
</ion-tab-button>
</ion-tab-bar>
Tab1.ts :
ionViewWillEnter() {
this.subscription = this.platform.backButton.subscribe(() => {
navigator['app'].exitApp();
});
}
Tab2.ts :
ionViewWillEnter() {
this.subscription = this.platform.backButton.subscribe(() => {
this.navCtrl.navigateRoot('/tabs');
});
}
In Tab2.ts navcontroller route well to /tabs (which mean, in the routing rules, go to tab1.)
Of course, in both case, subscription is unsubscribe in an ionViewDidLeave lifecycle hook.
But anyway, how to make the tab2 icon unselected when hardware back button is used to navigate to tab1 ?
It's like if the tabbar was notified that tab1 has been selected (because Tab1 icon is automatically selected), but tabbar wasn't notified to unselect Tab2 icon. That mean I have two icons selected in tabbar.
Thanks
After tracing what happens on actual devices, it appears that some phones (observed on Samsung S20+, 10+ and not on Motorola G9 for instance) set the :hover element state to the tab button when you tap it. Using the OS back navigation doesn't interact with the app UI and leaves the tab in that hovered state.
Ionic defines this CSS causing the tab to appear active on hover, which is fine if you are actually hovering it with your mouse:
#media (any-hover: hover) a:hover {
color: var(--color-selected);
}
To fight that, add this to your CSS:
ion-tab-button:not(.tab-selected)::part(native):hover {
color: var(--color);
}
Explanation: we're targeting the <a part="native"> tag inside tab buttons that are not selected but are hovered and restore their color to current text color.
If you want to keep the hover effect on desktop, you can combine the above rule with :not(.plt-desktop).
As to why a phone may have a hover effect without having a mouse, the question remains open.
In Ionic 5 the following worked for me:
ion-tab-button:not(.tab-selected) {
--color-selected: var(--color);
}
Related
I got two menu on the header menu. A left side "Burger Menu" and a right side menu with "add" symbol. The html content for the two menu are included in a seperate side-menu.html [not on app.html]
id for the left menu is "menuLeft" and the id for right menu as "menuRight"
The Burger menu appears on the main page and upon proceeding to a child menu , its replaced with a BACK button. This is expected and I am happy with that.
The Right menu is not enabled in any of the pages. I included the below code on only one of the child menu html file where the right menu needs to be appear.
<ion-navbar>
<ion-title>{{name}}</ion-title>
<button ion-button end>
<ion-icon name="add"></ion-icon>
</button>
</ion-navbar>
In its corresponding *.ts file I enabled the right menu in the ionViewWillEnter
ionViewWillEnter() {
this.menu.enable(true, 'menuRight');
}
Now, I have two issues.
1. When I include menuToggle in the button tag, the menu disappears. But I want the menu to appear and I should have the menu Toggle feature
I am able to make the right menu appear only when menuToggle attribute not mentioned.And still, it is not appearing on the right end of the header, it appears just prior to the Title. Refer screenshot attached.
Not sure why I am getting the background for the "add" button. Can I make it transparent?
Kindly let me know if you need more info. Thanks in advance
Change your navbar to make the menu appears on the right side, and i need more information about to awnser the other questions. Where are you placing the menuToggle ?
<ion-navbar>
<ion-title>{{name}}</ion-title>
<ion-buttons right>
<button ion-button>
<ion-icon name="add"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
When i open my app, the 1st page which appears is Login.html , after filling the form, it navigates to dashboard.html
Problem: When it goes to Dashboard.html , it shows back arrow on the top left and not the menu icon. I want it to show menu icon here and not the back arrow
This is the code i am using for navigation in login.ts:
if(data[0].USR==1)
{
this.navCtrl.push(DashboardPage);
}
Image of dashboard.html, please notice the arrow on left side
I have to click this arrow > go back to login > then from menu icon, i open menu and select dashboard page, then i see menu icon on dashboard.. why so ?
I have also created a video , sharing its youtube link - http://youtu.be/1eA5KXJkSDE?hd=1
It's default of Ionic when you navigate using push
If you wanna show menu toggle icon, you should use setRoot
Example:
onClickNavigate() {
console.log("onNavigate");
this.navCtrl.setRoot("PageTwo")
}
in "PageTwo" html, you need to add toggle button on the header too
<ion-header>
<ion-navbar>
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>page-two</ion-title>
</ion-navbar>
</ion-header>
Seems like you want to set the DashboardPage as a root page, so instead of the push method, use setRoot:
if(data[0].USR == 1) {
this.navCtrl.setRoot(DashboardPage);
}
I'm developing an ionic app on android. I'm facing a problem where back button doesn't show up when there's no page to go back.
For more detailed explanation:
Scenario 1: Button from side menu when click go to View B.
Scenario 2: Button from side menu to View A, then button from View A to View B.
Scenario 2 View B shows back button, since it has a previous page, but Scenario 1 doesn't have a previous page that's why it doesn't show the back button, How do I display the menu button if there's no back button?
Here's my code below:
<ion-view view-title="MY View">
<ion-nav-bar>
<ion-nav-back-button></ion-nav-back-button>
<ion-nav-buttons side="right">
<button class="button" type="submit"
ng-click="goEdit(data.ID)">Edit</button>
</ion-nav-buttons>
</ion-nav-bar>
<ion-content class="has-header">
</ion-content>
</ion-view>
Additional Info:
When removing the <ion-nav-bar> it displays the menu button, but of course will no longer show back button and edit button. When I try putting ng-hide in <ion-nav-bar ng-hide="isMenu"> it doesn't show any nav-bar since it's hidden but from html inspect element it's still there but hidden only. Any work around on this?
If you set a page as a rootPage and also use menu in you will see menu button. when navigating to another page from root if you use navCtrl.push() back button will automatically be added to the pushed view.if you set second page as a rootPage again you will see menu button again.
But if you want to implement it yourself that is another thing.
also check this link
Consider customizing the navbar/toolbar only for the pages that needs it.
By having an ion-toolbar in the ion-header, it appears on top of the default ion-navbar. So it is a workaround to have a custom header bar with my close icon and my custom function gotoHome(). That's the best way i found to customize the 'navbar' for a particular page.
<ion-header>
<ion-toolbar>
<ion-buttons left>
<button ion-button icon-only (click)="gotoHome()">
<ion-icon name="close"></ion-icon>
</button>
</ion-buttons>
<ion-title>Title</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding>
...
</ion-content>
The same answer applies to this topic, for reference :
Ionic Change back button icon for one page only
When i make hide-nav-bar="true" it also makes my sidebar menu disappear in the other pages i didnt wanted to. I just want to make the nav and side menu go away in the home page, but in the other pages my side menu is replaced for a back arrow for some reason. how can i solve this?
This is how it looks
This is how it is supposed to be
My code
<ion-view hide-nav-bar="true" title="Home" id="page1">
<ion-content padding="true" class="has-header backg">
<img class="log" src='../../img/image2.png' alt="HTML5 Icon" style="width:90px;height:90px;">
</ion-content>
</ion-view>
Please take a look at this plunker.
I just want to make the nav and side menu go away in the home page
In order to do that, you can, first, avoid including a header in your view. By just including and ion-content element in your home page html code, that view is not gonna have a navbar.
<ion-content>
<p>Home page</p>
<!-- ... -->
<!-- ... -->
</ion-content>
Even though we're not showing the navbar, the user could open the side menu by slicing it from the left (in this case) so we need to make sure to avoid that from happening like this:
Add an id to the ion-menu element like this:
<ion-menu [content]="content" side="left" id="menu">
<ion-toolbar secondary>
<ion-title>Menu</ion-title>
</ion-toolbar>
<ion-content>
<ion-list>
<button ion-item menuClose="menu" detail-none>
Close Menu
</button>
</ion-list>
</ion-content>
</ion-menu>
And then in your HomePage.ts disable it like this:
import { MenuController, ... } from 'ionic-angular';
#Component({
templateUrl:"home.html"
})
export class HomePage {
constructor(private menuCtrl: MenuController, ...) { }
ionViewDidEnter() {
this.menuCtrl.enable(false, 'menu');
}
// ...
}
in the other pages my side menu is replaced for a back arrow for some
reason.
That's related to the navigation array and how Ionic2 handles it. If you push a new page, that back arrow will be shown. Even though you can hide it, if the app is being run in an android device with a physical back button, the user will still be able togo back to the home page. If you don't want to let the user go back to the HomePage (because is the login page or something like that) use the setRoot method instead.
this.nav.setRoot(NewPage);
When navigating from one page to another, Ionic automatically display back button at the navigation bar. However, there are certain case where ionic don't display the back button. For example, when you navigate from a tab page to a none tab page.
How can I force Ionic to display back button on certain page?
Javascript:
.state('app.tab.playlists', { //<!-- Tab content page
url: '/playlists',
views: {
'tab-Content': {
templateUrl: 'templates/playlists.html',
controller: 'PlaylistsCtrl'
}
}
})
.state('app.singer', { //<!-- Not tab content page (if you navigate from tab page to this page, back button will not displayed)
url: '/singer',
views: {
'menuContent': {
templateUrl: 'templates/singer.html'
}
}
})
You can tell it in your controller. Try:
.controller('yourCtrl', function($scope) {
$scope.$on('$ionicView.beforeEnter', function (event, viewData) {
viewData.enableBack = true;
});
})
But like LeftyX said. The history function for tab to non tab view is buggy.
Ionic manages a history while you're navigating from one view to the other.
$ionicHistory keeps track of views as the user navigates through an
app. Similar to the way a browser behaves, an Ionic app is able to
keep track of the previous view, the current view, and the forward
view (if there is one). However, a typical web browser only keeps
track of one history stack in a linear fashion.
Unlike a traditional browser environment, apps and webapps have
parallel independent histories, such as with tabs. Should a user
navigate few pages deep on one tab, and then switch to a new tab and
back, the back button relates not to the previous tab, but to the
previous pages visited within that tab.
There's a bug open on github which hasn't been fixed yet (and will only be fixed in 2.0) where Adam Bradley states:
The back button does not display because when you go into a tab, it
enter's it own "history", meaning each tab has its own navigation back
and forward.
So, basically, when you move from tabs to regular view you're going to lose the back button.
What you can do is to create one yourself:
<ion-nav-buttons side="left">
<button class="button back-button buttons button-clear header-item" ng-click="goBack()">
<i class="icon ion-ios-arrow-back"> Back</i>
</button>
</ion-nav-buttons>
and place it inside your view:
<ion-view view-title="home">
<ion-nav-buttons side="left">
<button class="button back-button buttons button-clear header-item" ng-click="goBack()">
<i class="icon ion-ios-arrow-back"> Back</i>
</button>
</ion-nav-buttons>
<ion-content padding='true' scroll='false' has-footer='false'>
<div class="card">
<div class="item item-text-wrap">
HOME PAGE
</div>
</div>
</ion-content>
</ion-view>
As you can see I've used ng-click="goBack()" for the button.
In your controller you simply would:
$scope.goBack = function(){
$ionicHistory.goBack();
}
don't forget to inject $ionicHistory in your controller.
PS: This is a over-simplified solution as it does not check if the history has got a backview:
$ionicHistory.viewHistory().backView
Add follwing lines inside your
<ion-navbar hideBackButton="true">
<button (click)="back()" class="back-button disable-hover bar-button bar-button-ios back-button-ios bar-button-default bar-button-default-ios show-back-button" ion-button="bar-button" ng-reflect-klass="back-button" ng-reflect-ng-class="back-button-ios" style=""><span class="button-inner"><ion-icon class="back-button-icon icon icon-ios back-button-icon-ios ion-ios-arrow-back" role="img" ng-reflect-klass="back-button-icon" ng-reflect-ng-class="back-button-icon-ios" aria-label="arrow back" ng-reflect-name="ios-arrow-back"></ion-icon><span class="back-button-text back-button-text-ios" ng-reflect-klass="back-button-text" ng-reflect-ng-class="back-button-text-ios">Back</span></span><div class="button-effect"></div></button>