Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
[iOS] Fix ScaledScreenSize values using Face Up device orientation (#…
Browse files Browse the repository at this point in the history
…13163)

* Added repro sample

* Fix the issue

Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>
  • Loading branch information
jsuarezruiz and jfversluis authored Jan 24, 2022
1 parent 3620bea commit 9c0131f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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}";
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue8833.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue10086.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13136.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11980.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13173.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue8282.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13164.cs" />
Expand Down
25 changes: 18 additions & 7 deletions Xamarin.Forms.Platform.iOS/IOSDeviceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9c0131f

Please sign in to comment.