[Bug] ScrollView content's padding not applied after changed in OnSizeAllocated #9644
Description
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
- Create a new Xamarin Forms solution with the blank app template
- 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>
- 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);
}
}
}
- Debug the app on Android (untested with iOS) with a device in portrait orientation and note the padding on startup for portrait orientation.
- Rotate the device to landscape orientation and note the padding.
- 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:
In step 5, rotate to landscape:
In step 6, rotate to portrait:
Reproduction Link
Workaround
Use Margin instead of Padding.