【发布时间】:2017-09-04 08:34:01
【问题描述】:
我有一个带有标签的页面,在标签中我只想要文本,在 android 中,文本垂直和水平居中,没有任何空格,但在 ios 版本中,文本顶部有一个空格,因为没有图标。 如何去除顶部的空白?
【问题讨论】:
标签: ios xamarin tabs xamarin.forms
我有一个带有标签的页面,在标签中我只想要文本,在 android 中,文本垂直和水平居中,没有任何空格,但在 ios 版本中,文本顶部有一个空格,因为没有图标。 如何去除顶部的空白?
【问题讨论】:
标签: ios xamarin tabs xamarin.forms
您可以为此使用自定义渲染器:
[assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTabBarRenderer))]
namespace MyProject.iOS.Renderers
{
public class CustomTabBarRenderer : TabbedRenderer
{
public override void ViewWillAppear(bool animated)
{
if (TabBar?.Items == null)
return;
// Go through our elements and change them
var tabs = Element as TabbedPage;
if (tabs != null)
{
for (int i = 0; i < TabBar.Items.Length; i++)
UpdateTabBarItem(TabBar.Items[i]);
}
base.ViewWillAppear(animated);
}
private void UpdateTabBarItem(UITabBarItem item)
{
if (item == null)
return;
// Set the font for the title.
item.SetTitleTextAttributes(new UITextAttributes() { Font = UIFont.FromName("Your-Font", 10) }, UIControlState.Normal);
item.SetTitleTextAttributes(new UITextAttributes() { Font = UIFont.FromName("Your-Font", 10) }, UIControlState.Selected);
// Moves the titles up just a bit.
item.TitlePositionAdjustment = new UIOffset(0, -2);
}
}
}
TitlePositionAdjustment 是您正在寻找的。如果需要,您还可以使用 SetTitleTextAttributes 方法更改字体大小。
【讨论】: