How to add an image/logo to the actionbar?
Is it possible to add an image (png) to the actionbar? I want to add my logo, instead if a normal title string.
Thanks Michael
@mschakulat currently not but we are aware of that requirement and see it is a valuable addition.
@mschakulat currently only way to do it is to make custom actionbar.
You can try something like this:
tabris.ui.set("background", "red");
tabris.ui.set("textColor", "white");
tabris.ui.set("statusBarTheme", "dark");
tabris.ui.set("toolbarVisible", false); // hide original actionbar
var page = new tabris.Page({
title: "Custom Actionbar",
topLevel: true
});
var drawer = new tabris.Drawer();
var cActionBar = new tabris.Composite({
layoutData: {left: 0, right: 0, top: 0, height: 56},
background: tabris.ui.get("background")
}).appendTo(page);
new tabris.ImageView({ // original drawer button replacement
image: {src: 'http://i.imgur.com/z2s7mAD.png', width: 24, height: 24 },
layoutData: {left: 16, centerY: 0 },
scaleMode: 'fill',
tintColor: tabris.ui.get("textColor")
}).appendTo(cActionBar).on('tap', function(){
drawer.open();
});
new tabris.ImageView({ // add logo or any image
image: {src: 'http://i.imgur.com/wWQvPV7.png', width: 56, height: 56 },
layoutData: {left: 'prev() 10', centerY: 0 },
scaleMode: 'fill'
}).appendTo(cActionBar);
new tabris.TextView({ // add page name
layoutData: {left: 'prev() 10', centerY: 0 },
text: page.get('title'),
textColor: tabris.ui.get("textColor"),
font: "bold 16px"
}).appendTo(cActionBar);
page.open();
You have to replace imgur links with local images and you can simulate actions same way like drawer replacement, but instead of left, position action replacements at right side in same composite.
@bev-on Works almost perfect, thank you very much. But the original actionbar is still showing, at least on iOS simulator (i didn't try on a device so far).
Seems to be addressed in this issue: https://github.com/eclipsesource/tabris-js/issues/597
@mschakulat Last time I tried to make custom Actionbar, Tabris.js version was 0.9.4, I had same problem and I completely forget about this issue. I dig out my old code and find out that trick was to add style: ["FULLSCREEN"] as page property. Best part is that it still works :+1: also this is not real fullscreen, it won't hide statusbar, only actionbar and this is exactly what we want.
tabris.ui.set("background", "red");
tabris.ui.set("textColor", "white");
tabris.ui.set("statusBarTheme", "dark");
tabris.ui.set("toolbarVisible", false); // hide original actionbar
var page = new tabris.Page({
title: "Custom Actionbar",
style: ["FULLSCREEN"], // this is our fix for iOS
topLevel: true
});
var drawer = new tabris.Drawer();
var cActionBar = new tabris.Composite({
layoutData: {left: 0, right: 0, top: 0, height: 56},
background: tabris.ui.get("background")
}).appendTo(page);
new tabris.ImageView({ // original drawer button replacement
image: {src: 'http://i.imgur.com/z2s7mAD.png', width: 24, height: 24 },
layoutData: {left: 16, centerY: 0 },
scaleMode: 'fill',
tintColor: tabris.ui.get("textColor")
}).appendTo(cActionBar).on('tap', function(){
drawer.open();
});
new tabris.ImageView({ // add logo or any image
image: {src: 'http://i.imgur.com/wWQvPV7.png', width: 56, height: 56 },
layoutData: {left: 'prev() 10', centerY: 0 },
scaleMode: 'fill'
}).appendTo(cActionBar);
new tabris.TextView({ // add page name
layoutData: {left: 'prev() 10', centerY: 0 },
text: page.get('title'),
textColor: tabris.ui.get("textColor"),
font: "bold 16px"
}).appendTo(cActionBar);
page.open();
This code is tested and working on both iOS and Android... No original actionbar is visible even if you change/close pages, but please note that old Android versions doesn't support tintColor and you have to change icon colors manually using Photoshop or something else. In my example drawer icon is black and all Android devices until Android 5.0 will ignore tintColor and show it as black.
If you want to simulate back button on non topLevel pages, you can replace drawer icon with back arrow icon and execute nonTopLevelPage.close(); on tap. It will act like normal back button on both iOS and Android.
Also if you want your custom action bar to look more native, use icons provided by Google https://www.google.com/design/icons/ and include different size icons for different accept ratio screens. Most popular accept ratios are 0.75, 1, 1.5, 2, 3 and 4. If you include them all, icons will look perfectly native and pixel perfect. This is one of the way to do it:
/*
You must add all icon sizes to your local folder ( /icons for example)
/icons/drawer_0.75.png (18x18 pixels)
/icons/drawer_1.png (24x24 pixels)
/icons/drawer_1.5.png (36x34 pixels)
/icons/drawer_2.png (48x48 pixels)
/icons/drawer_3.png (72x72 pixels)
/icons/drawer_4.png (96x96 pixels)
*/
var sRatio = tabris.device.get('scaleFactor');
if (sRatio !== 0.75 && sRatio !== 1 && sRatio !== 1.5 && sRatio !== 2 && sRatio !== 3 && sRatio !== 4){
sRatio = 4; // use largest icon for weird non standard accept ratios
}
// this will load perfect pixel icon using sRatio
new tabris.ImageView({ // original drawer button replacement
image: {src: './icons/drawer_' + sRatio.toString() + '.png', width: 24, height: 24 },
layoutData: {left: 16, centerY: 0 },
scaleMode: 'fill'
});
// you can use this method for any image you want to look pixel perfect, not just icons
To hide the toolbar at the top there is the property toolbarvisible that can be used to control the toolbar. In conjunction with displaymode several effects can be achieved.
My last comment didn't account for the issue listed in #597 so it should be taken with a grain of salt.
@mpost what about my solution? I think we can use style: ["FULLSCREEN"] as page property as workaround until issue #597 is fixed. Can you test code from my last post in this issue and let us know if it's legit workaround? I can't find any issue, works perfect on all devices.
@bev-on As you rightfully stated it is an undocumented legacy property that will be removed in the future. If it works for now on all platforms we should strive to not remove it until the underlying issue is fixed on iOS.
@bev-on Thank you very much, this works perfect! Please, could you give me a hint, how to add content to the page, below the actionbar? I tried to append a CollectionView to the page but it seems it overrides the actionbar as it disappears. The CollectionView is there but not the Actionbar.
new tabris.CollectionView( { ... } ).appendTo(page)
@mschakulat set CollectionView layout properly
layoutData:{
top: [cActionBar, 10],
left: 0,
right: 0,
bottom: 0
}
Support for images on pages has been added in the 1.10 release.