dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.21k stars 1.75k forks source link

Scrollview with ScrollView VerticalOptions="Center" not growing to fit contents on iOS #23084

Open krime81 opened 4 months ago

krime81 commented 4 months ago

Description

When a ScrollView has dynamic contents ( in this example, a view that is growing in size, or made visible ) it doesnt grow to fit its contents on iOS, but it does on Android

Steps to Reproduce

Create a xaml file with following contents `<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="mauiscrollissue.MainPage">

<ScrollView VerticalOptions="Center" Background="Red" Padding="10">
    <VerticalStackLayout BackgroundColor="Aqua"
        Padding="30,0"
        Spacing="25">
        <Frame x:Name="hiddenAtFirst" BackgroundColor="Green" HeightRequest="200" IsVisible="false"/>
        <Frame BackgroundColor="Blue" HeightRequest="200" IsVisible="true"/>

        <Button
            x:Name="MakeVisibleButton"
            Text="Click me to grow" 
            Clicked="OnMakeVisibleButtonClicked"
            HorizontalOptions="Fill" />
    </VerticalStackLayout>
</ScrollView>

`

with codebehind : `namespace mauiscrollissue;

public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); }

private void OnMakeVisibleButtonClicked(object sender, EventArgs e)
{
    hiddenAtFirst.IsVisible = true;
    hiddenAtFirst.HeightRequest += 100;
}

} `

On tapping the button, -on Android the scrollview grows until it reaches the max height, and then shows the scrolling behavior -on iOS the scrollview doesnt grow and immediately shows scrolling behavior

Link to public reproduction project repository

https://github.com/krime81/mauiscrollissue

Version with bug

9.0.0-preview.1.9973

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

add this handler : ` Microsoft.Maui.Handlers.ScrollViewHandler.Mapper.AppendToMapping(nameof(IScrollView.ContentSize), (h, v) => { var contentSize = h.VirtualView.ContentSize;

            if (contentSize.IsZero)
                return;

            UIKit.UIScrollView uiScrollView = h.PlatformView;
            var container = uiScrollView.Subviews.FirstOrDefault(x => x.Tag == 0x845fed);           

            if (container != null && container.Bounds.Height != contentSize.Height)
            {
                container.Bounds = new CoreGraphics.CGRect(
                    container.Bounds.X,
                    container.Bounds.Y, 
                    contentSize.Width, 
                    contentSize.Height);

                (h.VirtualView as IView).InvalidateMeasure();
            }
        });
            `

note that this might affect other instances of the scrollview in your projects

Relevant log output

No response

github-actions[bot] commented 4 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

RoiChen001 commented 4 months ago

Can repro this issue at iOS platform on the latest 17.10.2(9.0.0-preview.5.24307.10) 23084