首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin.Forms.Shell:如何获得TabBar的底部高度?

Xamarin.Forms.Shell:如何获得TabBar的底部高度?
EN

Stack Overflow用户
提问于 2020-11-18 11:43:28
回答 2查看 1.3K关注 0票数 5

我在一个Xamarin.Forms.Shell应用程序上使用默认的底部TabBar,我需要知道TabBar 来调整一些项目。

我已经找到了在两个平台上获得StatusBar for 的方法,但是没有找到TabBar的解决方案。

有可能吗?我只找到了更改堆栈上的TabBar高度的请求.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-18 13:15:53

我们可以使用自定义渲染器获得高度

在AppShell中

代码语言:javascript
复制
public partial class AppShell : Xamarin.Forms.Shell
{
   public static double TabHeight { get; set; }

  //...
}

在iOS项目中

选项1:

代码语言:javascript
复制
using App33;
using App33.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(AppShell), typeof(ShellCustomRenderer))]
namespace App33.iOS
{
    public class ShellCustomRenderer : ShellRenderer
    {
        protected override IShellTabBarAppearanceTracker CreateTabBarAppearanceTracker()
        {
            return new TabBarAppearance();
        }

    }

    public class TabBarAppearance : IShellTabBarAppearanceTracker
    {
        public void Dispose()
        {

        }

        public void ResetAppearance(UITabBarController controller)
        {
            


        }

        public void SetAppearance(UITabBarController controller, ShellAppearance appearance)
        {
            base.ViewWillAppear(animated);

            UITabBar myTabBar = this.TabBarController.TabBar;

            myTabBar.TintColor = UIColor.Red;
            myTabBar.BarTintColor = UIColor.LightGray;
            //... you can set style like above in iOS

            AppShell.TabHeight  = myTabBar.Bounds.Height;

        }

        public void UpdateLayout(UITabBarController controller)
        {
           
        }
    }
}

备选方案2:

实际上,UITabbar在iOS上的高度是一个固定值。在iPhone 8和之前是49和iPhone X和之后(全屏)将有一个额外的safeArea底部高度。因此,如果您只想得到它(不需要设置高度),可以使用DependencyService直接获得它,如下所示

代码语言:javascript
复制
AppShell.TabHeight = 49 + UIApplication.SharedApplication.Delegate.GetWindow().SafeAreaInsets.Bottom;

在Android中

代码语言:javascript
复制
 public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);
    //        var hei=SupportActionBar.Height;
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());


            int resourceId =Resources.GetIdentifier("design_bottom_navigation_height", "dimen", this.PackageName);
            int height = 0;
            if (resourceId > 0)
            {
                height = Resources.GetDimensionPixelSize(resourceId);
                AppShell.TabHeight = height;
            }

         


        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}
票数 5
EN

Stack Overflow用户

发布于 2020-11-18 13:14:07

据我所知,在Xamarin.Forms中还没有(?)可以检索TabBar Height,因此您可能必须按照Platform收集该信息。然后,利用DependencyService,您可以从Xamarin.Forms共享代码中获得这些信息。那么,让我们看看如何做到这一点:

iOS

对于iOS,已经发布了像this这样的答案来解释如何做到这一点。

安卓

对于Android,您可以按以下方式检索TabBar高度

  • 为依赖项

创建接口

注意:这个接口也应该用于iOS部件;)

代码语言:javascript
复制
namespace Tabbarheight
{
    public interface IDisplayHeights
    {
        float GetTabBarHeight();
    }
}

  • Android中创建一个类,该类实现该接口并返回所需的值

(感谢@LucasZhang-MSFT的标识符字符串!)

代码语言:javascript
复制
using Android.App;
using Tabbarheight.Droid;
using Xamarin.Forms;

[assembly: Dependency(typeof(AndroidDisplayHeights))]
namespace Tabbarheight.Droid
{
    class AndroidDisplayHeights : IDisplayHeights
    {
        public static Activity Activity { get; set; }

        public float GetTabBarHeight()
        {
            int resourceId = Activity.Resources.GetIdentifier("design_bottom_navigation_height", "dimen", Activity.PackageName);
            int height = 0;
            if (resourceId > 0)
            {
                height = (int)(Activity.Resources.GetDimensionPixelSize(resourceId) / Xamarin.Essentials.DeviceDisplay.MainDisplayInfo.Density);
            }

            return height;

        }
    }
}

其中,MainActivity.cs设置Activity,如下所示

代码语言:javascript
复制
protected override void OnCreate(Bundle savedInstanceState)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(savedInstanceState);

    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

    AndroidDisplayHeights.Activity = this;

    LoadApplication(new App());
}

  • ,最后,您可以使用如下

代码语言:javascript
复制
public AboutPage()
{
    InitializeComponent();

    SizeChanged += (s, a) =>
    {
        label.HeightRequest = DependencyService.Get<IDisplayHeights>().GetTabBarHeight();
        label.BackgroundColor = Color.Red;

    };

}

在上面的解决方案中,我定义了一个Label (标签),它只是试图通过设置TabBar的高度来演示获取的LabelHeight值的准确性。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Tabbarheight.Views.AboutPage"
             xmlns:vm="clr-namespace:Tabbarheight.ViewModels"
             Title="{Binding Title}">

    <StackLayout>
        <Label x:Name="label">
            <Label.Text>
                <x:String>
                    Hola mundo
                </x:String>
            </Label.Text>
        </Label>
    </StackLayout>

</ContentPage>

我这边的标签是这样的:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64892392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档