C# Xamarin表单-ios中居中页面标题的自定义呈现程序
在Xamarin表单中,我尝试将页面标题左对齐,我知道我需要一个自定义的渲染器,但我不确定在下面的代码之后该去哪里 这甚至可能不正确,但我认为我需要创建一个页面渲染器并在那里进行对齐C# Xamarin表单-ios中居中页面标题的自定义呈现程序,c#,xamarin.forms,custom-renderer,C#,Xamarin.forms,Custom Renderer,在Xamarin表单中,我尝试将页面标题左对齐,我知道我需要一个自定义的渲染器,但我不确定在下面的代码之后该去哪里 这甚至可能不正确,但我认为我需要创建一个页面渲染器并在那里进行对齐 [assembly: ExportRenderer(typeof(Page), typeof(gl_mobile_app.iOS.TitleExRenderer))] namespace gl_mobile_app.iOS { public class TitleExRenderer : Xamarin.F
[assembly: ExportRenderer(typeof(Page), typeof(gl_mobile_app.iOS.TitleExRenderer))]
namespace gl_mobile_app.iOS
{
public class TitleExRenderer : Xamarin.Forms.Platform.iOS.PageRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
// align title
}
}
}
这一点在过去几年中没有得到苹果的官方支持 您可以尝试一个技巧:在TopItem.LeftBarButtonim中放置一个标签,并且不要使用默认的Title属性 在自定义渲染器中类似于以下内容:
var label = new UILabel();
//this is the variable containing the title you need to pass it as a bindable property
label.Text = title;
label.TextAlignment = UITextAlignment.Left;
label.SizeToFit();
NavigationController.NavigationBar.TopItem.LeftBarButtonItem = new UIBarButtonItem(label);
使现代化
您也可以阅读这篇优秀的博文:
更新2
您可以尝试获取默认页面标题,在左侧位置使用它,然后将其丢弃,如下所示:
var label = new UILabel();
//get the default title
label.Text = NavigationController.NavigationBar.TopItem.Title;
label.TextAlignment = UITextAlignment.Left;
label.SizeToFit();
//empty the default title (try with empty string or null if empty doesnt work)
NavigationController.NavigationBar.TopItem.Title = "";
NavigationController.NavigationBar.TopItem.LeftBarButtonItem = new UIBarButtonItem(label);
这是未经测试的,请告诉我是否有效:)您好,谢谢,如果我硬编码标题,我可以让它工作,但是有没有办法在将页面标题设置为null时使用实际页面标题?我似乎无法访问呈现程序中的标题(显然做错了)。我用一个技巧更新了答案,以获取默认页面标题并在左侧位置使用它。再次感谢,我尝试过,但我的问题是我将标题设置为OnAppearing(从数据库数据),渲染器在OnAppearing之前运行,所以只需在构造函数中设置标题,一切都很好