I am using the ionic starter menubar template. I would like to change the header background color of each page. I currently have:
<ion-view view-title="Search"> <ion-content> <h1>Search</h1> </ion-content> </ion-view>
I tried:
<ion-view view-title="Search" class="bar bar-header bar-assertive"> <ion-content> <h1>Search</h1> </ion-content> </ion-view>
But it does not work at all (content is not rendered). The header documentation does not help me. What is the correct way to do this?
You can change the background color of the toolbar with the standard title by setting the --background CSS variable on ion-toolbar . This will give the effect of the header changing color as you collapse the large title.
Styling the Ionic 5 Toolbar ( ion-toolbar ) $toolbar-background: #123456; $toolbar-border-color: #123456; $toolbar-text-color: #123456; $toolbar-active-color: #123456; $toolbar-inactive-color: #123456; Just put them in the variables. scss file and change their values to your desired colors.
A color can be applied to an Ionic component in order to change the default colors using the color attribute. Notice in the buttons below that the text and background changes based on the color set. When there is no color set on the button it uses the primary color by default.
Some ways to do this:
You could add the ion-nav-bar to each view.
<ion-view view-title="Page 1"> <ion-nav-bar class="bar-balanced"> <ion-nav-back-button></ion-nav-back-button> </ion-nav-bar> <ion-content> ... </ion-content> </ion-view>
Codepen example
You could also update the background-color (and any other properties) by using ng-style
Main navbar:
<ion-nav-bar class="bar-positive" ng-style="{'background-color': viewColor}"> <ion-nav-back-button></ion-nav-back-button> </ion-nav-bar>
CSS:
.nav-bar-block, .bar { background-color: inherit !important; }
Controller:
$scope.$on('$ionicView.beforeEnter', function() { $rootScope.viewColor = 'green'; });
Codepen example
Could not find a clean solution for this, but one hack might be to use the $stateChangeStart
event and set the class name manually.
angular.module('starter') .run(function ($rootScope) { var element; $rootScope.$on('$stateChangeStart', function (event, next) { if (next.name) { element = angular.element(document.querySelectorAll('ion-header-bar')); switch(next.name) { case 'tab.chats': element.removeClass('bar-positive'); element.removeClass('bar-balanced'); element.addClass('bar-calm'); break; case 'tab.dash': element.removeClass('bar-calm'); element.removeClass('bar-balanced'); element.addClass('bar-positive'); break; default : element.removeClass('bar-calm'); element.removeClass('bar-positive'); element.addClass('bar-balanced'); } } }); });
fiddle
EDIT The idea is same for sidebar template,
Updated fiddle
Notice the line
<ion-nav-bar class="bar-positive">
in menu.html template, it denotes the base header color class. but subsequent changes to pages i.e states header color needs to be changed manually in $stateChangeStart
event,
code:
.run(function ($rootScope) { var element; $rootScope.$on('$stateChangeStart', function (event, next) { if (next.name) { element = angular.element(document.querySelectorAll('ion-side-menu-content ion-header-bar')); console.log(element); switch(next.name) { case 'app.search': element.removeClass('bar-positive'); element.removeClass('bar-energized'); element.removeClass('bar-dark'); element.addClass('bar-assertive'); break; case 'app.browse': element.removeClass('bar-calm'); element.removeClass('bar-assertive'); element.removeClass('bar-dark'); element.addClass('bar-energized'); break; default : element.removeClass('bar-positive'); element.removeClass('bar-energized'); element.removeClass('bar-assertive'); element.addClass('bar-dark'); } } }); });
app.search
ionic color options
hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With