【问题标题】:IOS Tabs Title without icons white space on the topIOS选项卡标题没有图标顶部空白
【发布时间】:2017-09-04 08:34:01
【问题描述】:

我有一个带有标签的页面,在标签中我只想要文本,在 android 中,文本垂直和水平居中,没有任何空格,但在 ios 版本中,文本顶部有一个空格,因为没有图标。 如何去除顶部的空白?

【问题讨论】:

    标签: ios xamarin tabs xamarin.forms


    【解决方案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 方法更改字体大小。

    【讨论】:

    • 正在工作,但我不能换行,我想要文本,然后是文本下方的日期,但如果我在日期之前给出 System.Environment.NewLine,日期就会消失。我在 PCL 中这样做:Title = Text+System.Environment.NewLine+date;
    • 另一件事,如果我用新数据更新页面,文本位置会更改为原始位置(没有自定义渲染的位置)。
    • Apple 不允许您访问底层标签来更改其 numberOfLines 属性,并且由于 UITabBarItem 不继承自 UIView,因此您无法遍历其视图层次结构来搜索它。所以多行是不可能的。
    • 关于第二个问题,你能帮忙吗?
    • 我不认识这种行为。你有任何代码来显示你正在做什么来触发它吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 2013-03-06
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多