2017-09-04 14 views
0

私はタブでタブを持っています。テキストのみが必要です。アンドロイドでは、テキストは縦と横の両方にセンタリングされていますが、白いスペースはありませんが、アイコンがないので、テキストの上部にある空白です。 どうすれば上の空白を削除できますか?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で次のようにしています: Title = Text + System.Environment.NewLine + date; – Phill

+0

もう1つのことは、ページを新しいデータで更新すると、テキストの位置が元の位置(カスタムレンダリングのない位置)に変わります。 – Phill

+0

アップルは基本的なラベルにアクセスしてnumberOfLinesプロパティを変更することはできません。またUITabBarItemはUIViewから継承しないため、ビュー階層を検索することはできません。だから、複数行はできません。 –

関連する問題