减少工具栏图标填充(Xamarin.Forms) [英] Reduce Toolbar Icon Padding(Xamarin.Forms)

查看:117
本文介绍了减少工具栏图标填充(Xamarin.Forms)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过Xamarin.Forms缩小导航栏上两个图标之间的间距?

Is there a way that the spacing between two icons on the Nav Bar can be reduced in Xamarin.Forms?

请参见下图以解决问题:

See the image below for the problem:

图标间距太大

此距离适合2个图标,但是使用3个图标时,它会占据导航栏的一半-可以以任何方式减少此距离吗?

This distance is fine for 2 icons, but when using 3, it takes up half of the Nav Bar - any way this can be reduced?

谢谢.

推荐答案

您可以通过创建NavigationRenderer或PageRenderer两种方法来做到这一点.您必须重新创建UIButton来替换每个工具栏项,并从Xamarin.Forms项中获取委托的图像.将CGREct设置为UIButton时,可以在此处调整高度和宽度属性.

You can do it two ways, by creating a NavigationRenderer, or a PageRenderer. You have to recreate UIButtons to replace each toolbar item, and take the delegate an image from the Xamarin.Forms item. When you set the CGREct to the UIButton, here is where you can adjust the height and width properties.

这是我使用PageRenderer的实现.

Here is my implementation using a PageRenderer.

[assembly: Xamarin.Forms.ExportRenderer(typeof(Xamarin.Forms.ContentPage), typeof(ContentPageRenderer))]
public class ContentPageRenderer : PageRenderer
{
     protected override void OnElementChanged(VisualElementChangedEventArgs e)
     {
         base.OnElementChanged(e);
     }

     public override void ViewWillAppear(bool animated)
     {
         base.ViewWillAppear(animated);

         var navigationItem = this.NavigationController.TopViewController.NavigationItem;

         foreach (var rightItem in navigationItem.RightBarButtonItems)
         {
             var button = new UIButton(new CGRect(0, 0, 50, 50));
             button.SetImage(rightItem.Image, UIControlState.Normal);

             FieldInfo fi = rightItem.GetType().GetField("clicked", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
             Delegate del = (Delegate)fi.GetValue(rightItem);
             button.TouchUpInside += (EventHandler)del;

             rightItem.CustomView = button;
        }
    }
}

注意:如果动态更改Xamarin.Forms页面中的工具栏项目,它将覆盖渲染更改.

NOTE: If you dynamically change the toolbar items in your Xamarin.Forms page, it will override the render changes.

希望这会有所帮助.

这篇关于减少工具栏图标填充(Xamarin.Forms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆