标签页栏中图标和标签之间的间距

时间:2019-07-29 19:29:33

标签: xamarin.forms xamarin.ios tabbedpage

2nd question:  How to remove lines after last list item and how  to change the color of the icon?

我有一个tabbedPage,在其中为每个标签分配文本和图标,如下所示:

this.Children.Add(new SignaturesTab() { Title = "Signature" , Icon 
= "sign_new@2x.png" });
this.Children.Add(new PhotosTab() { Title = "Photos", Icon = 
"image_new@2x.png" });

在我的iPhone上,每个选项卡的图标都显示在栏中标签的顶部。

Tab Renderer中的代码:

protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
        TabBar.TintColor = new UIColor(red: 0.23f, green: 0.56f, blue: 0.20f, alpha: 1.0f);
        TabBar.UnselectedItemTintColor = new UIColor(red: 0.34f, green: 0.34f, blue: 0.34f, alpha: 1.0f);
    }

public override void ViewDidAppear(bool animated)
    {
        base.ViewDidAppear(animated);
        if (TabBar.Items == null) return;
        TabBar.SelectedImageTintColor = new UIColor(red: 0.23f, green: 0.56f, blue: 0.20f, alpha: 1.0f);
        foreach (var uiTabBarItem in TabBar.Items)
        {
            var fontSize = new UITextAttributes(){ Font = UIFont.SystemFontOfSize(13)};
            uiTabBarItem.SetTitleTextAttributes(fontSize, UIControlState.Normal);
        } 
    }

有没有办法在图标和标签之间以及边框之间提供间距/边距。

谢谢

2 个答案:

答案 0 :(得分:0)

使用 UIOffset UIEdgeInsets 可以修改TitleImageTabBarItem的位置。

TabBarItem.TitlePositionAdjustment = new UIOffset(0, 1);
//UIOffset:Represents a position offset. UIOffset(nfloat horizontal, nfloat vertical);

TabBarItem.ImageInsets = new UIEdgeInsets(0, 0, 5, 0);
//Mean: UIEdgeInsets(nfloat top, nfloat left, nfloat bottom, nfloat right);

修改UIOffsetUIEdgeInsets中的参数以适合您的需求。

======================================更新======== =====================

更改标签栏项目图标的颜色:

UITabBar.Appearance.SelectedImageTintColor = UIColor.Yellow;
//selected color ,this will change the whole tabbar item 

只需修改四个项目:

for (int i = 0; i < 4; i++)
{
   var fontSize = new UITextAttributes(){ Font = UIFont.SystemFontOfSize(13)};
   uiTabBarItem.SetTitleTextAttributes(fontSize, UIControlState.Normal);
   uiTabBarItem.TitlePositionAdjustment = new UIOffset(0, 1);
   uiTabBarItem.ImageInsets = new UIEdgeInsets(0, 0, 5, 0);
}

答案 1 :(得分:0)

为此,我使用IBDesignable进行了欺骗。 可能对您有用

import UIKit

@IBDesignable
class CustomTabBarItem: UITabBarItem {

    @IBInspectable var titlePosition: CGSize = CGSize(width: 0, height: 0) {
        didSet {
            titlePositionAdjustment = UIOffset(horizontal: titlePosition.width, vertical: titlePosition.height)
        }
    }
    
    @IBInspectable var imageInset: CGRect = CGRect(x: 0, y: 0, width: 0, height: 0) {
        didSet {
            imageInsets = UIEdgeInsets(top: imageInset.origin.x, left: imageInset.origin.y, bottom: imageInset.size.width, right: imageInset.size.height)
        }
    }
}