2017-09-04 89 views
0

我有一个带有标签的页面,在标签中我只想要文本,在android中文本垂直和水平居中,没有任何空格,但是在ios版本中是文本顶部的白色空间,因为没有图标。 我怎样才能删除顶部的空白?IOS标签没有图标的标题顶部的空白

回答

1

您可以使用自定义渲染器:

[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方法更改字体大小。

+0

正在工作,但我不能给一个换行符,我想要文本,然后在文本下面的日期,但如果我给日期之前的System.Environment.NewLine,日期消失。 我这样做在PCL: 标题=文本+ System.Environment.NewLine +日期; – Phill

+0

另一件事,如果我用新数据更新页面,文本位置变为原始位置(没有自定义渲染的位置)。 – Phill

+0

Apple不允许您访问底层标签以更改其numberOfLines属性,并且由于UITabBarItem不会从UIView继承,因此您无法遍历其视图层次结构来搜索它。所以多行是不可能的。 –