Xamarin.Forms: Remove indentation when using a dis

2019-08-09 17:50发布

问题:

I want to use a disclosure button on my table view cell. Therefore I use this custom renderer:

using HelloXamarinFormsWorld;
using HelloXamarinFormsWorld.iOS;
using UIKit;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using System.Drawing;
using ObjCRuntime;

/* Example of using a custom renderer to get the > disclosure indicator to appear */
[assembly: ExportRenderer (typeof (EmployeeCell), typeof (EmployeeCellRenderer))]

namespace HelloXamarinFormsWorld.iOS
{
    public class EmployeeCellRenderer : ViewCellRenderer
    {
        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            var cell = base.GetCell (item, reusableCell, tv);

            cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;

//          // Remove seperator inset
//          if (cell.RespondsToSelector (new Selector ("setSeparatorInset:"))) {
//              cell.SeparatorInset = UIEdgeInsets.Zero;
//          }
//          // Prevent the cell from inheriting the Table View's margin settings
//          if (cell.RespondsToSelector (new Selector ("setPreservesSuperviewLayoutMargins:"))) {
//              cell.PreservesSuperviewLayoutMargins = false;
//          }
//          // Explictly set your cell's layout margins
//          if (cell.RespondsToSelector (new Selector ("setLayoutMargins:"))) {
//              cell.LayoutMargins = UIEdgeInsets.Zero;
//          }

            return cell;
        }
    }
}

If I set the disclosure indicator I get the following screen:

Normally it does look like the following

I tried to play with the inset, but that has to be in WillDisplay and also the indentation is not removed. In this thread rmarinho states

you can fix by setting the UIEdgeInsets i think , i use the Padding property on ExtendedViewCell for this.

but I don't know where I should do this. Or how do I fix the indentation?

Edit:

Using cell.SeparatorInset = new UIEdgeInsets(0,0,0,0); didn't help.

回答1:

I guess it should be a bug in Xamarin.Forms.

After looking into the decompiled code, I have found that the default ViewCellRenderer tries to center itself by calculating the content view of the cell, which is not correct as it didn't consider the size of DisclosureIndicator.

So, the workaround is to insert an indicator manually (that's what ExtendedViewCell did) and do not use the default disclosure indicator.