iOS ScrollView should not scroll out of place on scrolling to element
authorAdrian Knight <adrianknight89@outlook.com>
Fri, 23 Sep 2016 05:32:32 +0000 (00:32 -0500)
committerAdrian Knight <adrianknight89@outlook.com>
Fri, 23 Sep 2016 05:32:32 +0000 (00:32 -0500)
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44461.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Platform.iOS/Renderers/ScrollViewRenderer.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44461.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44461.cs
new file mode 100644 (file)
index 0000000..c0937a8
--- /dev/null
@@ -0,0 +1,49 @@
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+
+namespace Xamarin.Forms.Controls
+{
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.Bugzilla, 44461, "ScrollToPosition.Center works differently on Android and iOS", PlatformAffected.iOS)]
+       public class Bugzilla44461 : TestContentPage
+       {
+               protected override void Init()
+               {
+                       var grid = new Grid
+                       {
+                               RowSpacing = 0,
+                       };
+
+                       var scrollView = new ScrollView
+                       {
+                               Orientation = ScrollOrientation.Horizontal,
+                               VerticalOptions = LayoutOptions.Center,
+                               BackgroundColor = Color.Yellow,
+                               HeightRequest = 50
+                       };
+                       grid.Children.Add(scrollView);
+
+                       var stackLayout = new StackLayout
+                       {
+                               Orientation = StackOrientation.Horizontal,
+                               Spacing = 20
+                       };
+
+                       for (var i = 0; i < 10; i++)
+                       {
+                               var button = new Button
+                               {
+                                       Text = "Button" + i
+                               };
+                               button.Clicked += (sender, args) =>
+                               {
+                                       scrollView.ScrollToAsync(sender as Button, ScrollToPosition.Center, true);
+                               };
+
+                               stackLayout.Children.Add(button);
+                       }
+                       scrollView.Content = stackLayout;
+                       Content = grid;
+               }
+       }
+}
\ No newline at end of file
index adf173d..92c4f8d 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla42364.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla42519.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla43516.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Bugzilla44461.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)CarouselAsync.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla34561.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla34727.cs" />
index 1e8ea1f..bfb2433 100644 (file)
@@ -208,6 +208,25 @@ namespace Xamarin.Forms.Platform.iOS
                        else
                        {
                                var positionOnScroll = Controller.GetScrollPositionForElement(e.Element as VisualElement, e.Position);
+
+                               if (positionOnScroll.X < 0)
+                               {
+                                       positionOnScroll.X = 0;
+                               }
+                               else if (positionOnScroll.X > ContentSize.Width - Bounds.Size.Width)
+                               {
+                                       positionOnScroll.X = ContentSize.Width - Bounds.Size.Width;
+                               }
+
+                               if (positionOnScroll.Y < 0)
+                               {
+                                       positionOnScroll.Y = 0;
+                               }
+                               else if (positionOnScroll.Y > ContentSize.Height - Bounds.Size.Height)
+                               {
+                                       positionOnScroll.Y = ContentSize.Height - Bounds.Size.Height;
+                               }
+
                                switch (ScrollView.Orientation)
                                {
                                        case ScrollOrientation.Horizontal: