From 9c0131f7b2369151f9ada6c20780b3b70291836f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Mon, 24 Jan 2022 15:43:43 +0100 Subject: [PATCH] [iOS] Fix ScaledScreenSize values using Face Up device orientation (#13163) * Added repro sample * Fix the issue Co-authored-by: Gerald Versluis --- .../Issue11980.cs | 60 +++++++++++++++++++ ...rin.Forms.Controls.Issues.Shared.projitems | 1 + Xamarin.Forms.Platform.iOS/IOSDeviceInfo.cs | 25 +++++--- 3 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue11980.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue11980.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue11980.cs new file mode 100644 index 00000000000..c585452f5cd --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue11980.cs @@ -0,0 +1,60 @@ +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +#if UITEST +using Xamarin.Forms.Core.UITests; +using Xamarin.UITest; +using NUnit.Framework; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ +#if UITEST + [Category(UITestCategories.ManualReview)] +#endif + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Github, 11980, "[iOS] ScaledScreenSize Height return wrong value when the Device Orientation is Faceup", + PlatformAffected.iOS)] + public class Issue11980 : TestContentPage + { + public Issue11980() + { + } + + protected override void Init() + { + Title = "Issue 11980"; + + var layout = new StackLayout(); + + var instructions = new Label + { + Padding = 12, + BackgroundColor = Color.Black, + TextColor = Color.White, + Text = "Using Face Up DeviceOrientation, tap the button. If the width and height values are different, the test has passed." + }; + + var button = new Button + { + Text = "Get ScaledScreenSize" + }; + + var heightLabel = new Label(); + var widthLabel = new Label(); + + layout.Children.Add(instructions); + layout.Children.Add(button); + layout.Children.Add(heightLabel); + layout.Children.Add(widthLabel); + + Content = layout; + + button.Clicked += (sender, args) => + { + heightLabel.Text = $"Height {Device.Info.ScaledScreenSize.Height}"; + widthLabel.Text = $"Width {Device.Info.ScaledScreenSize.Width}"; + }; + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index 9ab750319c5..0d53007f239 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -1789,6 +1789,7 @@ + diff --git a/Xamarin.Forms.Platform.iOS/IOSDeviceInfo.cs b/Xamarin.Forms.Platform.iOS/IOSDeviceInfo.cs index 4187d6881c3..b5687113ca8 100644 --- a/Xamarin.Forms.Platform.iOS/IOSDeviceInfo.cs +++ b/Xamarin.Forms.Platform.iOS/IOSDeviceInfo.cs @@ -30,18 +30,29 @@ void UpdateScreenSize() { _scalingFactor = UIScreen.MainScreen.Scale; - var boundsWidth = UIScreen.MainScreen.Bounds.Width; var boundsHeight = UIScreen.MainScreen.Bounds.Height; + var boundsWidth = UIScreen.MainScreen.Bounds.Width; + + double height; + double width; // We can't rely directly on the MainScreen bounds because they may not have been updated yet // But CurrentOrientation is up-to-date, so we can use it to work out the dimensions - var width = CurrentOrientation.IsLandscape() - ? Math.Max(boundsHeight, boundsWidth) - : Math.Min(boundsHeight, boundsWidth); + if (CurrentOrientation == DeviceOrientation.Other) + { + height = boundsHeight; + width = boundsWidth; + } + else + { + width = CurrentOrientation.IsLandscape() + ? Math.Max(boundsHeight, boundsWidth) + : Math.Min(boundsHeight, boundsWidth); - var height = CurrentOrientation.IsPortrait() - ? Math.Max(boundsHeight, boundsWidth) - : Math.Min(boundsHeight, boundsWidth); + height = CurrentOrientation.IsPortrait() + ? Math.Max(boundsHeight, boundsWidth) + : Math.Min(boundsHeight, boundsWidth); + } _scaledScreenSize = new Size(width, height); _pixelScreenSize = new Size(_scaledScreenSize.Width * _scalingFactor, _scaledScreenSize.Height * _scalingFactor);