How can I prevent content in the Xamarin Forms ScrollView from getting stuck on orientation change?

Let's say you have a Xamarin Forms ScrollView control with some content.

The content is such that; it fits completely into the viewport when the device is in portrait orientation, but it extends out of the viewport when the device is in landscape orientation.

Problem

When you rotate the device to landscape and scroll the content to the bottom, then rotate it back to portrait, the content is not reset and cannot be scrolled - it is now stuck halfway up the page.

This looks like a bug in Xamarin Forms as it only happens on iOS and not Android. However, this may take some time until it is resolved. I am looking for suggestions on how to prevent / overcome this situation on average? Perhaps with a custom renderer?

Here's a solution to reproduce the issue: https://github.com/afalz/ScrollViewIssue

+3


source to share


1 answer


I see that you are using a shared project (Xamarin.Forms). When programming in an IOS project, you can set the ContentSize of your ScrollView to accommodate for layout change (landscape, portrait).

ScrollView scrollview = new ScrollView();
scrollview.ContentSize = new RectangleF(x, y, screenHeight, screenWidth);

      

Then you can use the overridden ViewDidRotate method and change the ContentSize property.



In your example, I think you'd be better off implementing a method that gets called every time you change the screen layout, just like the ViewDidRotate method, and change the ScrollView.Content.HeightRequest to new screens.

Hope this helps. Good luck!

0


source







All Articles