From f8d7358144a96af0ab76a1b2715de8ecebab2225 Mon Sep 17 00:00:00 2001 From: Andrei Date: Mon, 18 Feb 2019 14:21:54 +0300 Subject: [PATCH] [MacOs] Slider inside StackLayout Horizontal Orientation crash (#5191) * fixes #3610 * fix https://github.com/xamarin/Xamarin.Forms/issues/3610 * updated default size * Added info comment --- .../Controls/FormsNSSlider.cs | 22 ++++++++++++++++++++++ .../Renderers/SliderRenderer.cs | 4 ++-- .../Xamarin.Forms.Platform.macOS.csproj | 1 + 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 Xamarin.Forms.Platform.MacOS/Controls/FormsNSSlider.cs diff --git a/Xamarin.Forms.Platform.MacOS/Controls/FormsNSSlider.cs b/Xamarin.Forms.Platform.MacOS/Controls/FormsNSSlider.cs new file mode 100644 index 0000000..719a081 --- /dev/null +++ b/Xamarin.Forms.Platform.MacOS/Controls/FormsNSSlider.cs @@ -0,0 +1,22 @@ +using AppKit; +using CoreGraphics; + +namespace Xamarin.Forms.Platform.macOS.Controls +{ + internal class FormsNSSlider : NSSlider + { + readonly CGSize _fitSize; + + internal FormsNSSlider() : base(CGRect.Empty) + { + Continuous = true; + SizeToFit(); + var size = Bounds.Size; + // This size will be set as default for horizontal NSSlider, if you try to create it via XCode (drag and drope) + // See this screenshot: https://user-images.githubusercontent.com/10124814/52661252-aecb8100-2f12-11e9-8f45-c0dab8bc8ffc.png + _fitSize = size.Width > 0 && size.Height > 0 ? size : new CGSize(96, 21); + } + + public override CGSize SizeThatFits(CGSize size) => _fitSize; + } +} diff --git a/Xamarin.Forms.Platform.MacOS/Renderers/SliderRenderer.cs b/Xamarin.Forms.Platform.MacOS/Renderers/SliderRenderer.cs index dcfe5ca..ca8b76d 100644 --- a/Xamarin.Forms.Platform.MacOS/Renderers/SliderRenderer.cs +++ b/Xamarin.Forms.Platform.MacOS/Renderers/SliderRenderer.cs @@ -1,7 +1,7 @@ using System; -using SizeF = CoreGraphics.CGSize; using AppKit; using System.ComponentModel; +using Xamarin.Forms.Platform.macOS.Controls; namespace Xamarin.Forms.Platform.MacOS { @@ -17,7 +17,7 @@ namespace Xamarin.Forms.Platform.MacOS { if (Control == null) { - SetNativeControl(new NSSlider { Continuous = true }); + SetNativeControl(new FormsNSSlider()); Control.Activated += OnControlActivated; } diff --git a/Xamarin.Forms.Platform.MacOS/Xamarin.Forms.Platform.macOS.csproj b/Xamarin.Forms.Platform.MacOS/Xamarin.Forms.Platform.macOS.csproj index 328b53c..15dd3ce 100644 --- a/Xamarin.Forms.Platform.MacOS/Xamarin.Forms.Platform.macOS.csproj +++ b/Xamarin.Forms.Platform.MacOS/Xamarin.Forms.Platform.macOS.csproj @@ -237,6 +237,7 @@ VisualElementRenderer.cs + DisposeHelpers.cs -- 2.7.4