简体   繁体   English

Xamarin.Forms ScrollView 添加额外空间

[英]Xamarin.Forms ScrollView adds extra space

My ScrollView adds extra spaces above the first child and under the last on my iPhone X. It is the blue gap above that Image.我的 ScrollView 在我的 iPhone X 上的第一个孩子上方和最后一个孩子下方添加了额外的空间。这是图像上方的蓝色间隙。 There is no padding or margin.没有填充或边距。

在此处输入图片说明

I gave the ScrollView a blue background to see it's dimensions better.我给了 ScrollView 一个蓝色背景以更好地查看它的尺寸。 If I remove the ScrollView, the spacing also is gone.如果我删除 ScrollView,间距也消失了。 Here`s the code I am using这是我正在使用的代码

        <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>
....

Is this maybe a bug or does it result by the iOS Statusbar rendering?这可能是一个错误还是由 iOS 状态栏渲染导致的? Also there is no spacing on my UWP project我的 UWP 项目也没有间距

After iOS 11, there is a property called contentInsetAdjustmentBehavior added to the scrollView.在 iOS 11 之后, contentInsetAdjustmentBehavior添加了一个名为contentInsetAdjustmentBehavior的属性。

From document :文件

This property specifies how the safe area insets are used to modify the content area of the scroll view.此属性指定如何使用安全区域插入来修改滚动视图的内容区域。

So, you need to use a custom renderer to config this property:因此,您需要使用custom renderer来配置此属性:

In code behiend:在代码隐藏中:

public class myScrollView : ScrollView {

}

In custom renderer:在自定义渲染器中:

[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;
            }
        }
    }
}

And in Xaml:在 Xaml 中:

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

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

Refer: uiscrollviewcontentinsetadjustmentbehavior参考: uiscrollviewcontentinsetadjustmentbehavior

custom-renderer 自定义渲染器

Row 1 is missing缺少第 1 行

Change this改变这个

<animatedViews:SavannahCanvasView HorizontalOptions="Center" Grid.Row="0" />
    <Grid Grid.Row="2" BackgroundColor="#fbc531">

To this对此

<animatedViews:SavannahCanvasView HorizontalOptions="Center" Grid.Row="0" />
    <Grid Grid.Row="1" BackgroundColor="#fbc531">`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM