Xamarin.Forms Add NavigationBar - xamarin.forms

I want to manage the nav bar appearence in the xaml?
And how can I manage the nav bar appearence in a Tabbed Page?
NavigationBar.SetHasNavigationBar(tabPage,true) seems not working.
Thank you all,
Ilenia

Maybe check that your mainpage is wrapped with NavigationPage in the public App() function:
MainPage = new NavigationPage(new LoginPage());

Changing the color of your navigation bar
To change the color of your navigation bar generically using Xamarin.Forms so that you only need to change the properties in one place, I'd suggest you add the following to your App.cs file like mentioned in this post on the Xamarin Forums.
Changing the color of the tab bar
Unfortunately, changing the color of the Tab Bar is currently not supported in xaml.
You will although need to write a customer renderer for iOS like so:
[assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTabbedPageRenderer))]
namespace JetAdvice_Free.iOS.Renderers
{
class CustomTabbedPageRenderer : TabbedRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
TabBar.TintColor = MonoTouch.UIKit.UIColor.Black;
TabBar.BarTintColor = MonoTouch.UIKit.UIColor.FromRGB(255, 128, 0);
}
}
}
Then, whenever you use a TappedPage on iOS, the background color will be applied to your tab bar.

You can set the navigationbar color only when you initialize your navigationpage like so:
new NavigationPage(your page here)
{
BarBackgroundColor = Color.Green,
BarTextColor = Color.White
};
For the issue of setting the tintcolor of the tabbar I recommend using a renderer like so:
public class TabPage_iOS : TabbedRenderer
{
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
this.TabBar.TintColor = Color.Blue.ToUIColor();
UITableView tableView = (UITableView)this.MoreNavigationController.TopViewController.View;
tableView.TintColor = this.TabBar.TintColor;
}
}

Related

How to Change Status Bar Background, Text Color for Single Content Page Xamarin (Ios, Android)

How to Change Status Bar Background, Text Color for Single Content Page.
I add:
styles.xml
I think it works on all Content Pages
I want:
Page1: Status Bar Background is Green.
Page2: Status Bar Background is Red.
...
Thank you!
How to Change Status Bar Background, Text Color for Single Content
Page Xamarin (Ios, Android)
If you want to change the Status Bar Background color for single page, you can use DependencyService to achieve this.
1.In xamarin forms,add interface IStatusBarPlatformSpecific
public interface IStatusBarPlatformSpecific
{
void SetStatusBarColor(Color color);
}
2.In android,add class ChangeStatusbar to implement interface IStatusBarPlatformSpecific:
[assembly: Dependency(typeof(ChangeStatusbar))]
namespace ToastApp.Droid
{
public class ChangeStatusbar : IStatusBarPlatformSpecific
{
public ChangeStatusbar()
{
}
public void SetStatusBarColor(Xamarin.Forms.Color color)
{
// The SetStatusBarcolor is new since API 21
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
var androidColor = color.AddLuminosity(-0.1).ToAndroid();
//Just use the plugin
Xamarin.Essentials.Platform.CurrentActivity.Window.SetStatusBarColor(androidColor);
}
else
{
// Here you will just have to set your
// color in styles.xml file as shown below.
}
}
}
}
Note:Android only supports black or white icons & text.
For more, check:How to customize status bar icons and text color? E.g. status bar background: white, status bar icon color, and text: red
3.In IOS,create class ChangeStatusbarIOS to implement interface IStatusBarPlatformSpecific:
[assembly: Dependency(typeof(ChangeStatusbarIOS))]
namespace ToastApp.iOS
{
public class ChangeStatusbarIOS : IStatusBarPlatformSpecific
{
public void SetStatusBarColor(Color color)
{
UIView statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
//statusBar.BackgroundColor = UIColor.Yellow;
statusBar.BackgroundColor = color.AddLuminosity(-0.1).ToUIColor();
UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);
}
}
}
4.In forms, you can change status backbround color for Single Content Page by override method OnAppearing and change back to it's original color by override method OnDisappearing:
protected override void OnAppearing()
{
base.OnAppearing();
var statusbar = DependencyService.Get<IStatusBarPlatformSpecific>();
statusbar.SetStatusBarColor(Color.Green);
}
protected override void OnDisappearing()
{
base.OnDisappearing();
var statusbar = DependencyService.Get<IStatusBarPlatformSpecific>();
statusbar.SetStatusBarColor(Color.FromHex("3F51B5"));
}

SetIcon change color

Hello i am trying to set icon of bottom nav bar
Icon
But everytime it changes color of whole icon and then it look like this
Icon
Is it possible to not give icon color so it wont change whole icon?
You could try to use the custom renderer to the set the icon. And set the ItemIconTintList to null.
[assembly: ExportRenderer(typeof(AppShell), typeof(ShellCustomRenderer))]
namespace App_Shell.Droid
{
class ShellCustomRenderer : ShellRenderer
{
public ShellCustomRenderer(Context context) : base(context)
{
}
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new CustomBottomNavAppearance();
}
}
public class CustomBottomNavAppearance : IShellBottomNavViewAppearanceTracker
{
public void Dispose()
{
}
public void ResetAppearance(BottomNavigationView bottomView)
{
}
public void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
bottomView.ItemIconTintList = null;
IMenu myMenu = bottomView.Menu;
IMenuItem myItemOne = myMenu.GetItem(0);
if (myItemOne.IsChecked)
{
myItemOne.SetIcon(Resource.Drawable.cactus_24px); // selected icon
}
else
{
myItemOne.SetIcon(Resource.Drawable.icon_about); //default icon
}
}
}
}
Update:
If you want to change the burger icon of shell, you could use Custom renderer to reset the icon vis CreateToolbarAppearanceTracker.
For more details, you could refer to the thread i done before. How to hide back button in navigation bar using Xamarin.Forms - AppShell?
If you want to change the back button color of navigation bar, you could set in style. Please check the link below. Change back button color in Xamarin.Android

xamarin forms background color of top status bar android

I'am using XamarinForms and I wanna know if I can set the backgroundColor using the configuration of the app.xaml, I'm using styles and themes that can be change at runtime, it is possible?
I'm using styles and themes that can be change at runtime, it is possible?
If you want to change status bar background at runtime, I suggest you can use DependencyService to change status bar background in Android platform.
Add this code to your Xamarin.Forms project(shared code project)
public interface IStatusBarPlatformSpecific
{
void SetStatusBarColor(Xamarin.Forms.Color color);
}
Implement the interface on Android platform. don't forget to register the platform implementations.
[assembly: Dependency(typeof(ChangeStatusbar))]
namespace FormsSample.Droid
{
public class ChangeStatusbar : IStatusBarPlatformSpecific
{
public void SetStatusBarColor(Xamarin.Forms.Color color)
{
// The SetStatusBarcolor is new since API 21
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
var androidColor = color.AddLuminosity(-0.1).ToAndroid();
//Just use the plugin
Xamarin.Essentials.Platform.CurrentActivity.Window.SetStatusBarColor(androidColor);
}
else
{
// Here you will just have to set your
// color in styles.xml file as shown below.
}
}
}
}
Now, you can change status bar background by calling the Get method to resolve the IStatusBarPlatformSpecific interface
private void btn1_Clicked(object sender, EventArgs e)
{
var statusbar = DependencyService.Get<IStatusBarPlatformSpecific>();
statusbar.SetStatusBarColor(Color.Green);
}
The screenshot:
The status bar should go the same colour as the navigation bar. You can set that to anything you like. I use styles to set this in a ContentPage as follows.
protected override void OnAppearing()
{
(this.Parent as NavigationPage).Style = (Style)Application.Current.Resources["NavBarColour"];
base.OnAppearing();
}
];
For Android modify Resources/Values/Styles.xml file as
<style name="MainTheme" parent="MainTheme.Base">
<item name="android:statusBarColor">#ffffff</item>
<item name="android:navigationBarColor">#ffffff</item>
</style>

How could I change the Navigastion's page arrow in Xamarin Forms?

I'm creating an app using xamarin Forms (multiplatform), I'm using a Navigation page, but I want to change the arrow ("<-") to text ("back")
Do you know how could i do it?
Thanks
(I'm going to use it in an Android App, but I'm creating the app using Xamarin forms)
You could use custom renderer to remove the navigation icon and set it with text. But, when you do that, you need to capture the click of the text and simulate the back event.
Create the interface:
public class CustomNavigationPage : NavigationPage
{
public CustomNavigationPage(Page startupPage) : base(startupPage)
{
}
}
The implementation of Android:
[assembly: ExportRenderer(typeof(CustomNavigationPage),
typeof(NavigationPageRenderer_Droid))]
namespace NavigationPageDemo.Droid
{
public class NavigationPageRenderer_Droid : NavigationPageRenderer
{
public Android.Support.V7.Widget.Toolbar toolbar;
public Activity context;
public NavigationPageRenderer_Droid(Context context) : base(context)
{
}
protected override Task<bool> OnPushAsync(Page view, bool animated)
{
var retVal = base.OnPushAsync(view, animated);
context = (Activity)Forms.Context;
toolbar = context.FindViewById<Android.Support.V7.Widget.Toolbar>(Droid.Resource.Id.toolbar);
if (toolbar != null)
{
//if (toolbar.NavigationIcon != null)
//{
//toolbar.NavigationIcon = Android.Support.V7.Content.Res.AppCompatResources.GetDrawable(context, Resource.Drawable.back);
//toolbar.NavigationIcon = null;
toolbar.NavigationIcon = null;
toolbar.Title = "back";
toolbar.SetOnClickListener(new OnClick());
//}
}
return retVal;
}
protected override Task<bool> OnPopViewAsync(Page page, bool animated)
{
return base.OnPopViewAsync(page, animated);
}
}
public class OnClick : Java.Lang.Object, IOnClickListener
{
void IOnClickListener.OnClick(Android.Views.View v)
{
App.Current.MainPage.Navigation.PopAsync();
}
}
In the custom renderer, use the OnClickListener to capture the click on text.
when you are working with xamarin forms it is suggested make use of common components and make least use of custom renderer.
Now for your requirement you want to create custom navigation bar
so here is how you can do it.
Create BaseContent Page
Create a Control Template inside your base page your can follow this link
Inside your control template using a grid view place your label with text binding (Back),also your can place a label in center to show title of page again u can make use of template binding which u would come to know when u go through the link
Now inherit your main page with your basecontentpage page
add your control template inside your main page
turn off your navigation bar of your main page
and you are done, this would give u more power to add more things like image or toolbar in your navbar
also to dynamically handle your back button u can check the count from navigationstack if its 0 u can show Humburger Icon or if its more than 0 u can show your label using IsVisible True/False

Change the color of the navigation bar

Iam developing a xamarin forms application.In my application,the navigation Bar color is blue. I want navigation bar color of one specific page as white.
I achieved that through
((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.White;
Now Iam facing a problem.The page which have white navigationbar will pop back to previous page after certain time.After the popasync, previous pages which have blue navigation bar will also turn to white color. How to avoid that?.
namespace sample
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ApprovedAnimation : ContentPage
{
public ApprovedAnimation ()
{
InitializeComponent ();
((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.White;
Device.StartTimer(TimeSpan.FromMilliseconds(3000), () =>
{
Navigation.PopAsync();
return false;
});
}
}
}
You need to set back the navigation color on the page itself using this method
protected override void OnDisappearing()
{
base. OnDisappearing();
((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.Blue; // this will set back the normal color
}

Resources