我正在使用Material Components在 iOS Xamarin 应用程序中,想要设置 MDCTabBar 的宽度指示更大的值,例如 8 dp。
据我了解,我应该向 SelectionIndicatorTemplate
提供实现 TabBarIndicatorTemplate
的自定义对象。
假设我初始化了标签栏:
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 崩溃:
=================================================================
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;
}
}
关于ios - 如何使用 Xamarin 在 iOS 上设置 Material Components 选项卡栏的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58237136/