[iOS] Fix scroll to does not scroll to end (#8241)
authorKonrad Müller <11095003+krdmllr@users.noreply.github.com>
Fri, 1 Nov 2019 18:50:32 +0000 (19:50 +0100)
committerRui Marinho <me@ruimarinho.net>
Fri, 1 Nov 2019 18:50:32 +0000 (18:50 +0000)
* Fix scroll to does not scroll to end

* Remove private modifier

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue2271.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/Issue2271.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue2271.cs
new file mode 100644 (file)
index 0000000..033762f
--- /dev/null
@@ -0,0 +1,102 @@
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+using System;
+using System.Linq;
+
+#if UITEST
+using Xamarin.UITest;
+using NUnit.Framework;
+#endif
+
+namespace Xamarin.Forms.Controls.Issues
+{
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.Github, 2271, "ScrollToAsync not working on iOS", PlatformAffected.iOS)]
+       public class Issue2271 : TestNavigationPage
+       {
+               public class LandingPage : ContentPage
+               { 
+                       StackLayout layout;
+                       Button addAtEndAndScrollToEnd, addAtStartAndScrollToStart, scrollToStart, scrollToEnd;
+                       ScrollView scrollView;
+
+                       public LandingPage()
+                       {
+                               layout = new StackLayout();
+                               for (var i = 0; i < 100; i++)
+                               {
+                                       layout.Children.Add(new Label { Text = $"This is a button {layout.Children.Count}" });
+                               }
+
+                               addAtEndAndScrollToEnd = new Button { Text = "Add Item and scroll to bottom" };
+                               addAtStartAndScrollToStart = new Button { Text = "Add item at beginning and move to beginning" };
+                               scrollToStart = new Button { Text = "Scroll to first" };
+                               scrollToEnd = new Button { Text = "Scroll to last" };
+                               scrollView = new ScrollView { Content = layout };
+
+                               Content = new StackLayout
+                               {
+                                       Children =
+                                       {
+                                               scrollView,
+                                               addAtEndAndScrollToEnd,
+                                               addAtStartAndScrollToStart,
+                                               scrollToStart,
+                                               scrollToEnd
+                                       }
+                               };
+
+                               addAtEndAndScrollToEnd.Clicked += AddItem_Clicked;
+                               addAtStartAndScrollToStart.Clicked += AddItemAtBegging_Clicked;
+                               scrollToEnd.Clicked += ScrollToEnd_Clicked;
+                               scrollToStart.Clicked += ScrollToStart_Clicked;
+                       }
+
+                       async void ScrollToStart_Clicked(object sender, EventArgs e)
+                       {
+                               await scrollView.ScrollToAsync(layout.Children.First(), ScrollToPosition.Start, false);
+                       }
+
+                       async void ScrollToEnd_Clicked(object sender, EventArgs e)
+                       {
+                               await scrollView.ScrollToAsync(layout.Children.Last(), ScrollToPosition.End, false);
+                       }
+
+                       async void AddItem_Clicked(object sender, EventArgs e)
+                       {
+                               Label lastButton = null;
+                               for (int i = 0; i < 10; ++i)
+                               {
+                                       lastButton = new Label
+                                       {
+                                               Text = $"Insert nr {layout.Children.Count}"
+                                       };
+                                       layout.Children.Add(lastButton);
+                               }
+
+                               await scrollView.ScrollToAsync(lastButton, ScrollToPosition.End, false);
+                       }
+
+                       async void AddItemAtBegging_Clicked(object sender, EventArgs e)
+                       {
+                               Label lastButton = null;
+                               for (int i = 0; i < 10; ++i)
+                               {
+                                       lastButton = new Label
+                                       {
+                                               Text = $"Insert nr {layout.Children.Count}"
+                                       };
+                                       layout.Children.Insert(0, lastButton);
+                               } 
+
+                               await scrollView.ScrollToAsync(lastButton, ScrollToPosition.Start, false);
+                       }
+               }
+
+               protected override void Init()
+               {
+                       var page = new LandingPage();
+                       Navigation.PushAsync(page);
+               }
+       }
+}
index df43eeb..670060a 100644 (file)
@@ -23,6 +23,7 @@
     <Compile Include="$(MSBuildThisFileDirectory)Issue3475.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue6476.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue7825.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Issue2271.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue5354.xaml.cs">
       <DependentUpon>Issue5354.xaml</DependentUpon>
       <SubType>Code</SubType>
index 7ca4e00..2a1b1e9 100644 (file)
@@ -267,9 +267,8 @@ namespace Xamarin.Forms.Platform.iOS
                        else
                        {
                                var positionOnScroll = ScrollView.GetScrollPositionForElement(e.Element as VisualElement, e.Position);
-
-                               positionOnScroll.X = positionOnScroll.X.Clamp(0, ContentSize.Width - Bounds.Size.Width);
-                               positionOnScroll.Y = positionOnScroll.Y.Clamp(0, ContentSize.Height - Bounds.Size.Height);
+                               positionOnScroll.X = positionOnScroll.X.Clamp(0, ContentSize.Width);
+                               positionOnScroll.Y = positionOnScroll.Y.Clamp(0, ContentSize.Height);
 
                                switch (ScrollView.Orientation)
                                {