CollectionView 在 iOS 上的高度意外增加

huangapple go评论62阅读模式
英文:

CollectionView takes unexpected height on iOS

问题

在将Xamarin.iOS更新到最新版本后,我意识到将CollectionView放入StackLayout并设置VerticalOptions = FillAndExpand会导致它占据无限的垂直空间,并最终排除在iOS和MacCatalyst上放置在其下的任何其他控件。
如果尝试删除FillAndExpand选项,它将占据预期的空间,但不会滚动到末尾。
如何实现正确的行为并保持下方控件的定位?

英文:

After updating Xamarin.iOS to the latest version, I realized that putting a CollectionView into a StackLayout with VerticalOptions = FillAndExpand would cause it to take endless vertical space and eventually rule out any other control that has been put below on iOS and MacCatalyst.
If I try to remove the FillAndExpand option, it will take the expected space but it won't scroll to the end.
How can I achieve the correct behavior keeping the below controls positioning?

答案1

得分: 2

StackLayout的扩展选项FillAndExpandMAUI中已被弃用。对于所有其他*AndExpand变体也是如此。虽然在Xamarin.Forms中很受欢迎,但在.NET MAUI中最终将不再起作用,因此应尽量避免使用。

另外,*AndExpand选项从未适用于除StackLayout之外的任何其他布局。在Xamarin.Forms和.NET MAUI中,它们不会对GridRelativeLayoutAbsoluteLayout产生任何影响。

如果您需要使项目填充指定数量的空间,应该使用Grid,然后设置VerticalOptions="Fill"

<Grid
    RowDefinitions="*"
    HeightRequest="300">

    <CollectionView
        Grid.Row="0"
        VerticalOptions="Fill"
        ItemsSource="{Binding MyItems}" />

</Grid>

这将创建一个具有特定高度的Grid,并且有一个占据Grid所有可用空间的单个行。放置在Grid内的CollectionView将填充其父项提供的可用空间。

重要提示:Grid具有VerticalOptions="Fill"而不是定义固定的HeightRequest时,也可以正常工作,但请确保它不包含在可滚动视图(例如ScrollView)中,因为在那种情况下可用空间实际上是无限的。

英文:

The StackLayout expansion option FillAndExpand is deprecated in MAUI. This is the case for all other *AndExpand variants, as well. Popular in Xamarin.Forms, they are not going to work anymore eventually in .NET MAUI and thus should be avoided, if possible.

Also, the *AndExpand options never applied to any other layout other than StackLayout. They don't have any effect in a Grid, RelativeLayout or AbsoluteLayout in Xamarin.Forms and .NET MAUI.

If you need an item to fill a defined amount of space, you should use a Grid and then set the VerticalOptions=&quot;Fill&quot; instead:

&lt;Grid
    RowDefinitions=&quot;*&quot;
    HeightRequest=&quot;300&quot;&gt;

    &lt;CollectionView
        Grid.Row=&quot;0&quot;
        VerticalOptions=&quot;Fill&quot;
        ItemsSource=&quot;{Binding MyItems}&quot; /&gt;

&lt;/Grid&gt;

This will create a Grid with a specific height and a single row which takes up all the available space of the Grid. The CollectionView placed inside the Grid will then fill the available space that its parent provides.

Important: This also works when the Grid has its VerticalOptions=&quot;Fill&quot; instead of defining a fixed HeightRequest, however, make sure that this then is not contained inside a scrollable view such as a ScrollView, because in that case the available space is virtually unlimited.

答案2

得分: 0

对于CollectionView要正确调整尺寸,以防止它占用过多垂直空间(最终排除下方的所有控件),在iOS上,它需要嵌套到一个Grid中,其中VerticalOptions = FillAndExpand。这将允许CollectionView滚动到末尾,并允许下方的任何其他控件按预期显示在View中。

英文:

For the CollectionView to be dimensioned correctly and to prevent it from taking to much vertical space (and eventually rule out all the controls below) on iOS, it needs to be nested into a Grid with VerticalOptions = FillAndExpand. This will let the CollectionView be scrolled to the end and will also allow any other control below to be shown as expected in the View.

huangapple
  • 本文由 发表于 2023年5月22日 15:06:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定