c# - Xamarin.Forms ScrollView 增加了额外的空间

标签 c# xaml xamarin xamarin.forms scrollview

我的 ScrollView 在我的 iPhone X 上的第一个子项上方和最后一个子项下方添加了额外的空间。它是该图像上方的蓝色间隙。没有填充或边距。

enter image description here

我为 ScrollView 设置了蓝色背景,以便更好地查看它的尺寸。 如果我删除 ScrollView,间距也会消失。这是我正在使用的代码

        <ScrollView>
            <!-- The MenuItems -->
            <Grid RowSpacing="0"
                  ColumnSpacing="0"
                  Margin="0, 0, 0, 0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="200" />
                    <RowDefinition Height="20" />
                    <RowDefinition Height="350" />
                    <RowDefinition Height="300" />
                    <RowDefinition Height="300" />
                    <RowDefinition Height="300" />
                    <RowDefinition Height="300" />
                    <RowDefinition Height="200" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <!--<ColumnDefinition Width="*" /> -->
                </Grid.ColumnDefinitions>
<animatedViews:SavannahCanvasView HorizontalOptions="Center"
                                                  Grid.Row="0" />

                <Grid Grid.Row="2"
                      BackgroundColor="#fbc531">
                    <Image HeightRequest="100"
                           VerticalOptions="End"
                           HorizontalOptions="FillAndExpand"
                           Aspect="Fill"
                           Source="{imageExtensions:ImageResource Source=Cheetah.Forms.Assets.Images.Background_Torque.png, TheAssembly=Cheetah.Forms}" />

                    <StackLayout VerticalOptions="Center"
                                 HorizontalOptions="Center"
                                 Margin="0,-100,0,0">

                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer Command="{Binding ShowMyPingsPageCommand}" />
                        </StackLayout.GestureRecognizers>

                        <Image Source="{imageExtensions:ImageResource Source=Cheetah.Forms.Assets.Images.Radar@256px.png, TheAssembly=Cheetah.Forms}"
                               HeightRequest="150"
                               WidthRequest="150" />
                        <Label VerticalOptions="Center"
                               HorizontalOptions="Center"
                               Style="{StaticResource WhiteLabel}"
                               Text="My Pings" />
                    </StackLayout>

                </Grid>
....

这可能是一个错误还是由 iOS 状态栏渲染导致的? 我的 UWP 项目也没有间距

最佳答案

在 iOS 11 之后,scrollView 添加了一个名为 contentInsetAdjustmentBehavior 的属性。

来自 document :

This property specifies how the safe area insets are used to modify the content area of the scroll view.

因此,您需要使用自定义渲染器 来配置此属性:

在代码隐藏中:

public class myScrollView : ScrollView {

}

在自定义渲染器中:

[assembly: ExportRenderer(typeof(myScrollView), typeof(MyScrollviewRender))]

namespace App308.iOS
{
    class MyScrollviewRender : ScrollViewRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null || Element == null)
            {
                return;
            }

            if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
            {
                this.ContentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentBehavior.Never;
            }
        }
    }
}

在 Xaml 中:

  <local:myScrollView>
        <!-- The MenuItems -->
        <Grid x:Name="myGrid" RowSpacing="0"
                  ColumnSpacing="0"
                  Margin="0, 0, 0, 0">

            ....
        </Grid>
    </local:myScrollView>

引用:uiscrollviewcontentinsetadjustmentbehavior

custom-renderer

关于c# - Xamarin.Forms ScrollView 增加了额外的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55862841/

相关文章:

Xamarin 在 XAML 中更改字体

c# - 带定时器的 Windows 服务

c# - Xamarin SQLite PCL 实现

c# - 以编程方式在 TabItem 中加载 UserControl

xaml - Xamarin.Forms 工具栏项目图标大小

c# - 如何在 XAML 中表达静态代码?

android - 在 MvxRecyclerView 中为选定项目设置动画

c# - WebRequest 将 URL 转换为 Xamarin 中的编码 (MonoTouch)

c# - 无法嵌入互操作类型 'ProjectKinds'。改用适用的接口(interface)

c# - 将 WinForm 放在右下角