#if IOS public class ButtonRenderer_IOS : Microsoft.Maui.Controls.Compatibility.Platform.iOS.ButtonRenderer { bool IsCalledForFirstTime = false; protected override void OnElementChanged((ElementChangedEventArgs<Button> e)) { base.OnElementChanged(e); if (Control != null) { Control.TitleLabel.LineBreakMode = UILineBreakMode.WordWrap; Control.TitleLabel.TextAlignment = UITextAlignment.Center; if (!IsCalledForFirstTime) { IsCalledForFirstTime = true; } } } } #endif
MAUI .NET 8 on iOS -System.ArgumentException: NaN is not a valid value for width
I have created a new MAUI .NET 8 project and migrated the Xamarin forms project code. When tried to run the project with the iOS simulator, which threw the exception below,
System.ArgumentException: NaN is not a valid value for width
at Microsoft.Maui.Graphics.Size..ctor(Double width, Double height)
at Microsoft.Maui.ViewHandlerExtensions.GetDesiredSizeFromHandler(IViewHandler viewHandler, Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[UIKit.UIView, Microsoft.iOS, Version=17.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].GetDesiredSize(Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Layouts.LayoutExtensions.ComputeDesiredSize(IView view, Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Controls.VisualElement.MeasureOverride(Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IView.Measure(Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Layouts.VerticalStackLayoutManager.Measure(Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Controls.StackLayoutManager.Measure(Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Controls.Layout.CrossPlatformMeasure(Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Platform.MauiView.CrossPlatformMeasure(Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Platform.MauiView.SizeThatFits(CGSize size)
at Microsoft.Maui.ViewHandlerExtensions.GetDesiredSizeFromHandler(IViewHandler viewHandler, Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.ILayout, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.LayoutView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].GetDesiredSize(Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Layouts.LayoutExtensions.ComputeDesiredSize(IView view, Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Controls.VisualElement.MeasureOverride(Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IView.Measure(Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Layouts.LayoutExtensions.MeasureContent(IContentView contentView, Thickness inset, Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Layouts.LayoutExtensions.MeasureContent(IContentView contentView, Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Controls.ContentPage.Microsoft.Maui.ICrossPlatformLayout.CrossPlatformMeasure(Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Platform.MauiView.CrossPlatformMeasure(Double widthConstraint, Double heightConstraint)
at Microsoft.Maui.Platform.MauiView.LayoutSubviews()
at Microsoft.Maui.Platform.ContentView.LayoutSubviews()
at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 58
at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94
at SKM_MOBILE.Program.Main(String[] args) in /Users/macmini3/Documents/MAUI/SKM_MOBILE/SKM_MOBILE/Platforms/iOS/Program.cs:line 13
In my Xamarin project, I used the CustomRender, I just wanted to use the same code in the converted MAUI project, so I added the below code in the MauiProgram.cs file as per this https://learn.microsoft.com/en-gb/dotnet/maui/migration/custom-renderers?view=net-maui-8.0 documentation,
handlers.AddCompatibilityRenderer(typeof(CustomButton), typeof(SKM_MOBILE.iOS.Renderers.ButtonRenderer_IOS));
public class ButtonRenderer_IOS : Microsoft.Maui.Controls.Compatibility.Platform.iOS.ButtonRenderer
{
bool IsCalledForFirstTime = false;
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.TitleLabel.LineBreakMode = UILineBreakMode.WordWrap;
Control.TitleLabel.TextAlignment = UITextAlignment.Center;
if (!IsCalledForFirstTime)
{
IsCalledForFirstTime = true;
Control.TouchDown += Control_TouchDown;
Control.TouchUpInside += Control_TouchUp;
Control.TouchDragExit += Control_TouchDragExit;
}
}
}
public CustomButton(ButtonStyle buttonType = ButtonStyle.SystemSpecific)
{
this.buttonStyle = buttonType;
switch (buttonType)
{
case ButtonStyle.Default:
{
BackgroundColor = Colors.Gray;
CornerRadius = 5;
TextColor = Colors.White;
}
break;
case ButtonStyle.LongTextButton:
{
BackgroundColor = Colors.Transparent;
TextColor = ApplicationButtonBGnTxtColor;
HorizontalOptions = LayoutOptions.Center;
BorderColor = ApplicationButtonBGnTxtColor;
BorderWidth = 2;
CornerRadius = 5;
FontSize = 15;
FontAttributes = FontAttributes.Bold;
Pressed += OnPressed;
Released += OnReleased;
SetAllCaps = false;
Padding = new Thickness(4, 0, 4, 0);
HeightRequest = 40;
}
break;
Did I miss anything? How do I resolve this issue?