Search code examples
ioslistviewxamarin.forms

Xamarin Forms: Blank space is showing on top of Listviews in ios platform


On top of my all listviews in a project, there is a blank space on the ios platform, no such issue on android or windows.

My code:

<ListView 
        x:Name="MyItems"
        RefreshCommand="{Binding RefreshCommand}"
        IsPullToRefreshEnabled="True"
        IsRefreshing="{Binding IsRefreshing}"
        HasUnevenRows="True">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.View>
                        <StackLayout
                            Orientation="Vertical"> 

                            <StackLayout
                                HorizontalOptions="FillAndExpand"
                                VerticalOptions="FillAndExpand"
                                Orientation="Horizontal">
                                
                                </StackLayout>
                            </StackLayout>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        <ListView.Footer>
            <Label/>
        </ListView.Footer>
    </ListView>

Additional Details:

  • XF version: 4.8.0.1821

  • Project type is Portable


Solution

  • Please see this thread : https://developer.apple.com/forums/thread/683980.

    To solve it we can add a custom renderer for ListView

    Try the following code :

        [assembly: ExportRenderer(typeof(ListView), typeof(LVRenderer))]
    namespace YourNameSpace.iOS
    {
        public class LVRenderer : ListViewRenderer
        {
            public LVRenderer()
            {
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
            {
                base.OnElementChanged(e);
                if (Control != null)
                {
                    Control.SectionHeaderTopPadding = new nfloat(0);
                }
            }
        }
    }
    

    Found it here :

    Weird space on top of the listview after updating iOS to 15