Daniel Hindrikes
Developer and architect with focus on mobile- and cloud solutions!
Developer and architect with focus on mobile- and cloud solutions!
If you're building an iOS app you maybe want to add content to the NavigationBar. This blog post will show you how to build a generic solution so you don't have to write a custom renderer for each view that you want NavigationBar content for.
First of all I creating a new class that inherits from ContentPage and adding a property of type View. I use to name the property TitleView, because that's the name in iOS.
public class ExtendedContentPage : Xamarin.Forms.ContentPage
{
public View TitleView { get; set; }
}
Now If your views/pages inherits from your new class you can add content to TitleView.
To get it work you also have to write a custom page renderer for iOS. Override the WillMoveToParentViewController method in the renderer that will inherit PageRenderer and add the code below.
base.WillMoveToParentViewController(parent);
var page = (ExtendedContentPage)Element;
var renderer = RendererFactory.GetRenderer(page.TitleView);
var view = renderer.NativeView;
view.SizeToFit();
parent.NavigationItem.TitleView = view.Subviews[0];
It is important that you adding the first subview of the native view, otherwise it will be just blank.
The complete code can be found on GitHub, https://github.com/dhindrik/XamarinFormsSamples/tree/master/TitleView