Daprin Wakaliana
Daprin Wakaliana

Reputation: 185

Xamarin Forms TableView Default Padding

I want to add ViewCell and SwitchCell inside a TableView. I want to know what is the default margin for SwitchCell so I can make the TableView looks beautiful. Can you help me?

    <TableView Intent="Form">
        <TableRoot>
            <TableSection>
                <ViewCell>
                    <StackLayout Padding="?, ?, ?, ?">
                        <!-- Something-->
                    </StackLayout>
                </ViewCell>
                <SwitchCell Text="Something"/>
            </TableSection>
        </TableRoot>
    </TableView>

Upvotes: 1

Views: 1285

Answers (2)

James Westgate
James Westgate

Reputation: 11444

In a custom renderer for a view cell on iOS, you get access to the TableView. The TableView has a SeparatorInset value for the inset of the separator, which handily is the same as the margin we require for the view cell.

This seems to work well across all the iPhone emulator images I tried (SE, iPhone XR, iPhone XS) on iOS 12.2.

public override UIKit.UITableViewCell GetCell(Cell item, UIKit.UITableViewCell reusableCell, UIKit.UITableView tv)
{
    var inset = tv.SeparatorInset.Left;
    var viewCell = (ViewCell) item;

    viewCell.View.Margin = new Thickness(inset, 0, inset, 0);

    return base.GetCell(item, reusableCell, tv);
}

Upvotes: 1

Pratik
Pratik

Reputation: 720

You can use Grid instead of ViewCell. Try like this

    <TableView Intent="Form">
        <TableRoot>
            <TableSection>
                <Grid>
                   <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="*" />
                     <ColumnDefinition Width="Auto" />
                  </Grid.ColumnDefinitions>
                    <StackLayout Grid.Column="0" Padding="?, ?, ?, ?">
                        <!-- Something-->
                    </StackLayout>
                    <SwitchCell Grid.Column="1" Text="Something"/>
                </Grid>                    
            </TableSection>
        </TableRoot>
    </TableView>

Upvotes: 2

Related Questions