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

[Bug] ScrollView content's padding not applied after changed in OnSizeAllocated #9644

Open
@Adam--

Description

@Adam--

Description

When a ScrollView contains a StackLayout (untested with other controls) and that StackLayout's padding is changed in OnSizeAllocated, the padding isn't visually shown (seems to be delayed until redrawing of the control).

Steps to Reproduce

  1. Create a new Xamarin Forms solution with the blank app template
  2. In MainPage.xaml use the following for it's content
<ScrollView>
        <!--  See code behind for Padding changes on rotation (OnSizeAllocated)  -->
        <StackLayout x:Name="StackLayout">
            <Label
                BackgroundColor="#DEDEDE"
                HorizontalOptions="Fill"
                HorizontalTextAlignment="Center"
                Text="Welcome to Xamarin.Forms!"
                VerticalOptions="CenterAndExpand" />
        </StackLayout>
    </ScrollView>
  1. In MainPage.xaml.cs override OnSizeAllocated with
// Reference https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/device-orientation?tabs=windows
        protected override void OnSizeAllocated(double width, double height)
        {
            base.OnSizeAllocated(width, height);

            if (width != _width || height != _height)
            {
                _width = width;
                _height = height;
                if (width > height)
                {
                    StackLayout.Padding = new Thickness(60, 0);
                }
                else
                {
                    StackLayout.Padding = new Thickness(10, 0);
                }
            }
        }
  1. Debug the app on Android (untested with iOS) with a device in portrait orientation and note the padding on startup for portrait orientation.
  2. Rotate the device to landscape orientation and note the padding.
  3. Rotate the device back to portrait orientation and not the padding.

Expected Behavior

In step 4, startup in portrait, the left/right padding is 10.
In step 5, rotate to landscape, the left/right padding is 60.
In step 6, rotate to portrait, the left/right padding is 10.

Actual Behavior

In step 4, startup in portrait, the left/right padding is 0 (Android 9) or 10 (Android 4.4) (should be 10).
In step 5, rotate to landscape, the left/right padding is 10 (should be 60).
In step 6, rotate to portrait, the left/right padding is 60 (should be 10).

Basic Information

  • Version with issue: 4.3.0.908675, 4.2.0.84806
  • Last known good version: Unknown
  • IDE: Visual Studio Professional 16.4.1
  • Platform Target Frameworks:
    • iOS: 8.0? (untested)
    • Android: 9.0
  • Nuget Packages:
    • NETStandard.Library v2.0.3
    • Xamarin.Essentials v1.3.1
    • Xamarin.Forms v4.3.0.908675

Screenshots

In step 4, startup in portrait:
4-startup-portrait

In step 5, rotate to landscape:
5-rotate-landscape

In step 6, rotate to portrait:
6-rotate-portrait

Reproduction Link

XFStackLayoutPaddingBug.zip

Workaround

Use Margin instead of Padding.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions