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

Fix "ScrollView resets position" issue during changing TranslationX/TranslationY #3842

Merged
merged 5 commits into from Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Threading.Tasks;


#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 3840, "[iOS] Translation change causes ScrollView to reset to initial position (0, 0)",
PlatformAffected.iOS)]
#if UITEST
[NUnit.Framework.Category(UITestCategories.ScrollView)]
#endif
public class Issue3840 : TestContentPage
{
const string _failedText = "Test Failed if Visible";
const string _button1 = "FirstClick";
const string _button2 = "SecondClick";

protected override void Init()
{
ScrollView scroll = null;
scroll = new ScrollView
{
Content = new StackLayout
{
Children =
{
new Label()
{
Text = _failedText
},
new Button()
{
Text = "Click Me First",
AutomationId = _button1,
Command = new Command(async () =>
{
await scroll.ScrollToAsync(0, 100, true);
}),
HorizontalOptions = LayoutOptions.Start
},
new BoxView { Color = Color.Red, HeightRequest = 500 },
new Button()
{
Text = "Click Me Second",
AutomationId = _button2,
Command = new Command(async () =>
{
scroll.TranslationX = 100;
await Task.Delay(100);
// using one because of a bug on UWP that doesn't react to being set back to zero
scroll.TranslationX = 1;

}),
HorizontalOptions = LayoutOptions.Start
},
new BoxView { Color = Color.Gray, HeightRequest = 500 },
new BoxView { Color = Color.Yellow, HeightRequest = 500 }
}
}
};

var mainLayout = new AbsoluteLayout();
mainLayout.Children.Add(scroll, new Rectangle(0, 0, 1, 1), AbsoluteLayoutFlags.All);
Content = mainLayout;
}


#if UITEST
[Test]
public void TranslatingViewKeepsScrollViewPosition()
{
RunningApp.WaitForElement(_failedText);
RunningApp.Tap(_button1);
RunningApp.Tap(_button2);
RunningApp.WaitForNoElement(_failedText);
}
#endif
}
}
Expand Up @@ -393,6 +393,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue3398.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue3558.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue3541.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue3840.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue3913.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LegacyComponents\NonAppCompatSwitch.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MapsModalCrash.cs" />
Expand Down Expand Up @@ -997,7 +998,7 @@
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue1588.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
Expand Down
13 changes: 9 additions & 4 deletions Xamarin.Forms.Platform.iOS/Renderers/ScrollViewRenderer.cs
Expand Up @@ -236,8 +236,8 @@ void HandleScrolled(object sender, EventArgs e)

void OnNativeControlUpdated(object sender, EventArgs eventArgs)
{
ContentSize = Bounds.Size;
UpdateContentSize();
var elementContentSize = RetrieveElementContentSize();
ContentSize = elementContentSize.IsEmpty ? Bounds.Size : elementContentSize;
}

void OnScrollToRequested(object sender, ScrollToRequestedEventArgs e)
Expand Down Expand Up @@ -287,11 +287,16 @@ void UpdateBackgroundColor()

void UpdateContentSize()
{
var contentSize = ((ScrollView)Element).ContentSize.ToSizeF();
var contentSize = RetrieveElementContentSize();
if (!contentSize.IsEmpty)
ContentSize = contentSize;
}

CoreGraphics.CGSize RetrieveElementContentSize()
{
return ((ScrollView)Element).ContentSize.ToSizeF();
}

void UpdateScrollPosition()
{
if (ScrollView != null)
Expand All @@ -303,4 +308,4 @@ void IEffectControlProvider.RegisterEffect(Effect effect)
VisualElementRenderer<VisualElement>.RegisterEffect(effect, this, NativeView);
}
}
}
}