Sreejith Sree
Sreejith Sree

Reputation: 3766

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:

Upvotes: 1

Views: 393

Answers (2)

Brenton Unger II
Brenton Unger II

Reputation: 11

The current accepted answer breaks on iOS 14.7.1, it creates the error "UIKit_UITableView_set_SectionHeaderTopPadding_System_nfloat - unrecognized selector sent to instance"

Here is a modified version that will work.

[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 && UIDevice.CurrentDevice.CheckSystemVersion(15, 0))
            {
                Control.SectionHeaderTopPadding = new nfloat(0);
            }
        }
    }
}

See why on this related issue: Value of type 'UITableView' has no member 'sectionHeaderTopPadding'

Also see the developer docs for sectionHeaderTopPadding which is denoted as iOS 15.0+: https://developer.apple.com/documentation/uikit/uitableview/3750914-sectionheadertoppadding

Upvotes: 1

Bas H
Bas H

Reputation: 2226

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

Upvotes: 2

Related Questions