簡體   English   中英

如何使用 Xamarin 在 iOS 上設置 Material Components 選項卡欄的寬度

[英]How to set width of Material Components tab bar on iOS with Xamarin

我在 iOS Xamarin 應用程序中使用材料組件,並希望將MDCTabBar指示器的寬度設置為更大的值,例如 8 dp。

據我了解,我應該將實現TabBarIndicatorTemplate的自定義 object 提供給SelectionIndicatorTemplate

所以假設我初始化了tabbar:

tabBar = new TabBar();
tabBar.Items = new UITabBarItem[] {
   new UITabBarItem("One", null, 0),
   new UITabBarItem("Two", null, 0)
};
tabBar.Alignment = TabBarAlignment.Justified;
tabBar.ItemAppearance = TabBarItemAppearance.Titles;
tabBar.TitleTextTransform = TabBarTextTransform.None;

然后使用我的自定義CustomTabBarIndicatorTemplate

tabBar.SelectionIndicatorTemplate = new CustomTabBarIndicatorTemplate();

實現如下:

private class CustomTabBarIndicatorTemplate : TabBarIndicatorTemplate
{
    public override TabBarIndicatorAttributes IndicatorAttributesForContext(ITabBarIndicatorContext context)
    {
        var bounds = context.Bounds;
        var attr = new TabBarIndicatorAttributes();
        var frame = new CGRect(bounds.GetMinX(), bounds.GetMaxY() - 8, bounds.Width, 8);
        attr.Path = UIBezierPath.FromRect(frame);
        return attr;
    }
}

不幸的是,這會導致 iOS 上的本機崩潰:

=================================================================
    Native Crash Reporting
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
    Native stacktrace:
=================================================================
    0x10dc02ae5 - /Users/user/Library/Developer/CoreSimulator/Devices/44BEABB1-FBEC-48AB-8CE3-F04C3969A12A/data/Containers/Bundle/Application/23FCADDC-6D6C-4DCA-8C0F-1D2CCF19D914/MyApp.iOS.app/MyApp.iOS : mono_dump_native_crash_info
    0x10dbf6a25 - /Users/user/Library/Developer/CoreSimulator/Devices/44BEABB1-FBEC-48AB-8CE3-F04C3969A12A/data/Containers/Bundle/Application/23FCADDC-6D6C-4DCA-8C0F-1D2CCF19D914/MyApp.iOS.app/MyApp.iOS : mono_handle_native_crash
    0x10dc09d61 - /Users/user/Library/Developer/CoreSimulator/Devices/44BEABB1-FBEC-48AB-8CE3-F04C3969A12A/data/Containers/Bundle/Application/23FCADDC-6D6C-4DCA-8C0F-1D2CCF19D914/MyApp.iOS.app/MyApp.iOS : mono_sigsegv_signal_handler_debug
    0x11e7c7b5d - /usr/lib/system/libsystem_platform.dylib : _sigtramp
    0x10dc820b9 - /Users/user/Library/Developer/CoreSimulator/Devices/44BEABB1-FBEC-48AB-8CE3-F04C3969A12A/data/Containers/Bundle/Application/23FCADDC-6D6C-4DCA-8C0F-1D2CCF19D914/MyApp.iOS.app/MyApp.iOS : mono_de_add_pending_breakpoints
    0x14593b306 - Unknown

=================================================================
    Basic Fault Address Reporting
=================================================================
Memory around native instruction pointer (0x11d03080a):0x11d0307fa  00 00 00 00 00 90 4c 8b 57 08 48 8b 3f 49 89 f3  ......L.W.H.?I..
0x11d03080a  45 23 5a 18 49 c1 e3 04 4d 03 5a 10 49 3b 33 75  E#Z.I...M.Z.I;3u
0x11d03081a  04 41 ff 63 08 49 83 3b 01 76 0d 49 83 c3 10 49  .A.c.I.;.v.I...I
0x11d03082a  3b 33 75 f1 41 ff 63 08 72 1b 4d 8b 5b 08 eb 0a  ;3u.A.c.r.M.[...

=================================================================
    Managed Stacktrace:
=================================================================
      at <unknown> <0xffffffff>
      at ApiDefinition.Messaging:IntPtr_objc_msgSendSuper <0x00135>
      at MaterialComponents.TabBarIndicatorTemplate:.ctor <0x00342>
      at CustomTabBarIndicatorTemplate:.ctor <0x00072>
      at MyApp.iOS.Views.[REDACTED].[REDACTED]ViewController:SetLayout <0x00a32>
      at MyApp.iOS.Views.BaseViewController`1:PrepareLayout <0x001b0>
      at MyApp.iOS.Views.BaseViewController`1:ViewDidLoad <0x000fa>
      at System.Object:runtime_invoke_void__this__ <0x001a5>
      at <unknown> <0xffffffff>
      at UIKit.UIApplication:UIApplicationMain <0x00251>
      at UIKit.UIApplication:Main <0x000b2>
      at UIKit.UIApplication:Main <0x00132>
      at MyApp.iOS.Application:Main <0x00092>
      at <Module>:runtime_invoke_void_object <0x001a8>
=================================================================

我應該如何正確實現這一點?

解決方案是調用基本構造函數base(NSObjectFlag.Empty)

private class CustomTabBarIndicatorTemplate : TabBarIndicatorTemplate
{
    public CustomTabBarIndicatorTemplate() : base(NSObjectFlag.Empty)
    {
        Console.WriteLine("Constructor");
    }

    public override TabBarIndicatorAttributes IndicatorAttributesForContext(ITabBarIndicatorContext context)
    {
        var bounds = context.Bounds;
        var attr = new TabBarIndicatorAttributes();
        var frame = new CGRect(bounds.GetMinX(), bounds.GetMaxY() - 8, bounds.Width, 8);
        attr.Path = UIBezierPath.FromRect(frame);
        return attr;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM