如果要将标题设置为多行。可以设置titleView的样式,这个功能我已经实现了,可以查看here。
首先,我们可以创建一个ContentPage的子类(BaseContentPage)
(在Android中,只需要设置TitleView的样式)
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class BaseContentPage : ContentPage
{
public BaseContentPage ()
{
InitializeComponent ();
if (Device.RuntimePlatform == "Android")
{
NavigationPage.SetHasBackButton(this, false);
NavigationPage.SetTitleView(this, SetTitleView("Morning \n 9.30 Am"));
}
else
{
Title = "Morning \n 9.30 Am";
}
}
StackLayout SetTitleView(string title)
{
Button button = new Button()
{
Text = "Back",
TextColor = Color.White,
FontAttributes = FontAttributes.None,
BackgroundColor = Color.Transparent,
};
button.Clicked += Button_Clicked;
StackLayout TitleView = new StackLayout()
{
Margin = new Thickness(-20, 0, 0, 0),
Orientation = StackOrientation.Horizontal,
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.Start,
Children = {
button,
new Label(){
Text = title,
TextColor = Color.White,
HorizontalTextAlignment = TextAlignment.Center,
WidthRequest = 200,
}
}
};
return TitleView;
}
private void Button_Clicked(object sender, EventArgs e)
{
Navigation.PopAsync();
}
}
因为此方法仅适用于 Android。所以你应该在 iOS 上做更多的事情。
在iOS项目中。创建BaseContentPage的自定义渲染器。并设置导航栏的样式。
using Foundation;
using UIKit;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using xxx.iOS;
using CoreGraphics;
using xxx;
using ObjCRuntime;
[assembly: ExportRenderer(typeof(BaseContentPage), typeof(MyPageRenderer))]
namespace xxx.iOS
{
public class MyPageRenderer: PageRenderer
{
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
var page = Element as Page1;
NavigationController.NavigationBar.Hidden = true;
double height = IsiphoneX();
UIView backView = new UIView()
{
BackgroundColor = UIColor.White,
Frame = new CGRect(0,20,UIScreen.MainScreen.Bounds.Width, height),
};
UIButton backBtn = new UIButton() {
Frame = new CGRect(20, height-44, 40, 44),
Font = UIFont.SystemFontOfSize(18),
} ;
backBtn.SetTitle("Back", UIControlState.Normal);
backBtn.SetTitleColor(UIColor.Blue, UIControlState.Normal);
backBtn.AddTarget(this,new Selector("GoBack"),UIControlEvent.TouchUpInside);
UILabel titleLabel = new UILabel() {
Frame=new CGRect(UIScreen.MainScreen.Bounds.Width/2-75, 0,150, height),
Font = UIFont.SystemFontOfSize(20),
Text = page.Title,
TextColor = UIColor.Black,
Lines = 0,
};
UILabel line = new UILabel() {
Frame = new CGRect(0, height, UIScreen.MainScreen.Bounds.Width, 0.5),
BackgroundColor = UIColor.Black,
};
backView.AddSubview(backBtn);
backView.AddSubview(titleLabel);
backView.AddSubview(line);
View.AddSubview(backView);
}
double IsiphoneX()
{
double height = 44;
if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
{
if (UIApplication.SharedApplication.Delegate.GetWindow().SafeAreaInsets.Bottom > 0.0)
{
height = 64;
}
}
return height;
}
[Export("GoBack")]
void GoBack()
{
NavigationController.PopViewController(true);
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
NavigationController.NavigationBar.Hidden = false;
}
}
}
结果是:
安卓:
IOS: