Skip to content
Merged
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ public class Bugzilla44461 : ContentPage

public Bugzilla44461()
{
var grid = new Grid
{
RowSpacing = 0,
};
var stackLayout = new StackLayout();


var instructions = new Label
{
Text = @"Tap the first button (Button0). The button should be aligned with the left side of the screen "
+ "and should not move. If it's not, the test failed."
};

grid.Children.Add(instructions);
stackLayout.Add(instructions);

var scrollView = new ScrollView
{
Expand All @@ -27,9 +25,19 @@ public Bugzilla44461()
BackgroundColor = Colors.Yellow,
HeightRequest = 50
};
grid.Children.Add(scrollView);

var stackLayout = new StackLayout

var scrollButton = new Button
{
Text = "Scroll to Button0",
AutomationId = "Scroll to Button0",
Margin = 10,
};

stackLayout.Add(scrollButton);
stackLayout.Add(scrollView);

var horizontalStack = new StackLayout
{
Orientation = StackOrientation.Horizontal,
Spacing = 20
Expand All @@ -47,10 +55,16 @@ public Bugzilla44461()
scrollView.ScrollToAsync(sender as Button, ScrollToPosition.Center, true);
};

stackLayout.Children.Add(button);
horizontalStack.Children.Add(button);
}
scrollView.Content = stackLayout;
Content = grid;

scrollButton.Clicked += (sender, args) =>
{
var button = horizontalStack.Children[0];
scrollView.ScrollToAsync(button as Element, ScrollToPosition.Center, true);
};
scrollView.Content = horizontalStack;
Content = stackLayout;
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#if TEST_FAILS_ON_IOS && TEST_FAILS_ON_CATALYST //When clicking Button0 (the first button) incorrectly centers it in the ScrollView instead of maintaining its left alignment.
//Issue Link: https://github.com/dotnet/maui/issues/26760
using NUnit.Framework;
using NUnit.Framework;
using NUnit.Framework.Legacy;
using UITest.Appium;
using UITest.Core;
Expand All @@ -18,24 +16,10 @@ public Bugzilla44461UITests(TestDevice device)
public override string Issue => "ScrollToPosition.Center works differently on Android and iOS";

[Test]
[FailsOnIOSWhenRunningOnXamarinUITest("This test is failing, likely due to product issue")]
[FailsOnMacWhenRunningOnXamarinUITest("This test is failing, likely due to product issue")]
public void Bugzilla44461Test()
{
var positions = TapButton(0);
Assert.That(positions.initialPosition.X, Is.EqualTo(positions.finalPosition.X));
Assert.That(positions.finalPosition.X, Is.LessThanOrEqualTo(1));
}

(System.Drawing.Rectangle initialPosition, System.Drawing.Rectangle finalPosition) TapButton(int position)
{
var buttonId = $"{position}";
App.WaitForElement(buttonId);
var initialPosition = App.WaitForElement(buttonId).GetRect();
App.Tap(buttonId);
var finalPosition = App.WaitForElement(buttonId).GetRect();
return (initialPosition, finalPosition);
App.Tap("Scroll to Button0");
VerifyScreenshot();
}
}
}
#endif
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions src/Core/src/Handlers/ScrollView/ScrollViewHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ public static void MapRequestScrollTo(IScrollViewHandler handler, IScrollView sc
return;
}

var availableScrollHeight = uiScrollView.ContentSize.Height - uiScrollView.Frame.Height;
var availableScrollWidth = uiScrollView.ContentSize.Width - uiScrollView.Frame.Width;
var minScrollHorizontal = Math.Min(request.HorizontalOffset, availableScrollWidth);
var minScrollVertical = Math.Min(request.VerticalOffset, availableScrollHeight);
var availableScrollHeight = Math.Max(uiScrollView.ContentSize.Height - uiScrollView.Frame.Height, 0);
var availableScrollWidth = Math.Max(uiScrollView.ContentSize.Width - uiScrollView.Frame.Width, 0);
var minScrollHorizontal = Math.Clamp(request.HorizontalOffset, 0, availableScrollWidth);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a heads up, I'm adapting this to fix the issue on my app via an overridden handler and ran into the following issue:

If the ContentSize Width of the scroll view is for whatever reason less than the width of the UIScrollView Frame's width, it causes an argument error. I'd suggest the following changes to account for it.

var minScrollHorizontal = Math.Clamp(request.HorizontalOffset, 0, Math.Max(availableScrollWidth, 0));
var minScrollVertical = Math.Clamp(request.VerticalOffset, 0, Math.Max(availableScrollHeight, 0));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gdougherty-cbt, I have updated the code section based on your suggestions.

var minScrollVertical = Math.Clamp(request.VerticalOffset, 0, availableScrollHeight);
uiScrollView.SetContentOffset(new CGPoint(minScrollHorizontal, minScrollVertical), !request.Instant);

if (request.Instant)
Expand Down
Loading