Bug 52510 - [iOS] ScrollView resets offset on device rotation.
Summary: [iOS] ScrollView resets offset on device rotation.
Alias: None
Product: Forms
Classification: Xamarin
Component: iOS ()
Version: 2.3.4
Hardware: Macintosh Mac OS
: High normal
Target Milestone: ---
Assignee: Chris King
Depends on:
Reported: 2017-02-14 20:23 UTC by Jon Goldberger [MSFT]
Modified: 2017-12-22 13:46 UTC (History)
8 users (show)

Tags: ac ios
Is this bug a regression?: ---
Last known good build:

Test Project (34.01 KB, application/zip)
2017-02-14 20:23 UTC, Jon Goldberger [MSFT]

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 52510 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Jon Goldberger [MSFT] 2017-02-14 20:23:26 UTC
Created attachment 19818 [details]
Test Project

## Description

If you scroll a ScrollView and then rotate the device, the scroll offset is reset so any scrolling done before rotation is lost and the ScrollView returns to a 0,0 offset. 

Issue is only on iOS, works properly on Android (did not test UWP). 

## Steps to reproduce

1. Open the attached test project  and deploy to iOS sim or device.

2. Scroll the view so you can no longer see the label "Top" at the top.

3. Rotate the sim/device

Expected result: ScrollView will remain scrolled to the same offset as before rotation.

Actual result: ScrollView scrolls back to the top, once again showing the "Top" label. 

## Notes

Issue is still present in Forms 2.3.4-pre2

## Workaround

A workaround is to create a custom ScrollView renderer and save the offset to a class variable whenever the view is scrolled and then subscribe to the iOS `UIDeviceOrientationDidChangeNotification` and set the UIScrollViews ContentOffset to the last saved offset:

>public class MyScrollViewRenderer : ScrollViewRenderer
>	{
>		CGPoint offset;
>		protected override void OnElementChanged(VisualElementChangedEventArgs e)
>		{
>			base.OnElementChanged(e);
>			UIScrollView sv = NativeView as UIScrollView;
>			sv.Scrolled += (sender, evt) =>
>			{
>				// Checking if sv.ContentOffset is not 0,0
>				// because the ScrollView resets the ContentOffset to 0,0 when rotation starts
>				// even if the ScrollView had been scrolled (I believe this is likely the cause for the bug).
>				// so you only want to set offset variable if the ScrollView is scrolled away from 0,0
>				// and I do not want to reset offset to 0,0 when the rotation starts, as it would overwrite my saved offset.
>				if (sv.ContentOffset.X != 0 || sv.ContentOffset.Y != 0)
>					offset = sv.ContentOffset;
>			};
>			// Subscribe to the oreintation changed event.
>			NSNotificationCenter.DefaultCenter.AddObserver(this, new Selector("handleRotation"), new NSString("UIDeviceOrientationDidChangeNotification"), null);
>		}
>		[Export("handleRotation")]
>		void HandleRotation()
>		{
>			if (offset.X != 0 || offset.Y != 0)
>			{
>				UIScrollView sv = NativeView as UIScrollView;
>				// Reset the ScrollView offset from the last saved offset.
>				sv.ContentOffset = offset;
>			}
>		}
>	}

## Environment

=== Xamarin Studio Enterprise ===

Version 6.2 (build 1809)
Installation UUID: ceaba76c-db06-4fbd-b326-f69ea53c3e01
	Mono 4.8.0 (mono-4.8.0-branch/ba7f169) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 408000483

=== NuGet ===


=== Xamarin.Profiler ===

Version: 1.1.8
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 8.2.1 (11766.1)
Build 8C1002

=== Xamarin.Mac ===

Version: (Visual Studio Enterprise)

=== Xamarin Inspector ===

Hash: 1f3067d
Branch: master
Build date: Tue, 15 Nov 2016 21:13:59 GMT

=== Xamarin.Android ===

Version: (Visual Studio Enterprise)
Android SDK: /Users/jongoldberger/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)
		7.0   (API level 24)

SDK Tools Version: 25.2.3
SDK Platform Tools Version: 25.0.1
SDK Build Tools Version: 25.0.1

Java SDK: /usr
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

Android Designer EPL code available here:

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Xamarin.iOS ===

Version: (Visual Studio Enterprise)
Hash: 22d559f
Branch: cycle9
Build date: 2017-02-03 16:04:59-0500

=== Build Information ===

Release ID: 602001809
Git revision: bd75dae9aa2e6041309bc6a4f6140cc099ea2e98
Build date: 2017-02-03 15:04:22-05
Xamarin addins: bf31ec767f2a60b3e34647baf33798b26c9336aa
Build lane: monodevelop-lion-cycle9

=== Operating System ===

Mac OS X 10.12.3
Darwin Jons-MacBook-Pro.local 16.4.0 Darwin Kernel Version 16.4.0
    Thu Dec 22 22:53:21 PST 2016
    root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64

=== Enabled user installed addins ===

Addin Maker 1.3.2
Prism Template Pack 1.5
Comment 2 adrianknight89 2017-02-17 16:19:42 UTC
Possibly related: https://bugzilla.xamarin.com/show_bug.cgi?id=43947