How do I adjust the left margin for prototype cell

2019-01-13 09:31发布

If I create a UITableViewController, for example via File → New Project... → iOS → Master-Detail Application in Xcode, a UITableView is created with a prototype cell.

The generated view hierarchy is:

UITableViewController view hierarchy

A left "margin" is automagically created between the Cell's Content UIView left edge and the "Title" text's UILabel element as shown below in orange.

Prototype cell gap

This results in a corresponding margin between the device's screen edge and the UILabel text at runtime:

Runtime gap

So, where is the width of this gap set, and how can it be adjusted?

The controls in the Size Inspector for the UILabel are greyed out:

enter image description here

My preferred option would to be able to set the width of this gap from within Interface Builder, but I would also like to understand where this gap is being set, and how to alter it programmatically.

10条回答
Melony?
2楼-- · 2019-01-13 10:26

You just need to set contentInset property of the table view. You can set value according to your need.

self.tableView.contentInset = UIEdgeInsetsMake(0, -15, 0, 0);

OUTPUT RESULT

查看更多
一夜七次
3楼-- · 2019-01-13 10:27

Go to Main.storyboard > select the UITableViewCell > Attributes Inspector. Change Separator dropdown list from Default Insets to Custom Insets. Change the left inset from 15 to 0

enter image description here

查看更多
老娘就宠你
4楼-- · 2019-01-13 10:31

If you're using a xib for your cell, be sure that the "Relative to margin" is unchecked. You can check this via the inspector as shown in the following screenshot:

enter image description here

查看更多
Juvenile、少年°
5楼-- · 2019-01-13 10:31

I think I just came up with an easy solution. lol.

The top answer have some problem...it decrease the left gap, but resulting a right gap.

I used the constrains in Interface Builder.

First add a -15 left margin constrain to the Table View.

Then add some Indentation to the Table Cell to make the contents look better.


Here're some step-by-step pics:

Add the constraint. Remember to uncheck the "spacing to nearest neighbor".

Add the constraint. Remember to uncheck the "spacing to nearest neighbor".

The Table Cells will move left. But seems too close to margin.

After adding the constraint

So choose the Table Cell, and add some indentation in the right area.

add some indentation

查看更多
登录 后发表回答