How do I adjust the left margin for prototype cells in a UITableView?

IosObjective CUitableviewInterface Builder

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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

http://i.stack.imgur.com/gzU7r.png" width="320">

Solution 2 - Ios

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

Solution 3 - Ios

Starting from iOS 8 is available the cell property layoutMargins. So the correct way to adjust cell margins is setting this property in your tableView:cellForRowAtIndexPath or in your custom UITableViewCell in this way:

override func awakeFromNib() {
    super.awakeFromNib()

    self.layoutMargins = UIEdgeInsetsZero //or UIEdgeInsetsMake(top, left, bottom, right)
    self.separatorInset = UIEdgeInsetsZero //if you also want to adjust separatorInset
}

I hope this can help someone.

Solution 4 - Ios

Just add the method in your code as mentioned in the below link

> https://stackoverflow.com/questions/27937855/how-do-i-eliminate-the-margin-on-the-left-side-of-a-uitableview-without-creatin/31579395#31579395

The method is like this in Objective-C:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
    [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
    cell.preservesSuperviewLayoutMargins = NO;
    [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
    [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

Swift 3 and 4

public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
        cell.separatorInset = .zero
    }
    if (cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins))) {
        cell.preservesSuperviewLayoutMargins = false
    }

    if (cell.responds(to: #selector(setter: UIView.layoutMargins))) {
        cell.layoutMargins = UIEdgeInsets.zero
    }
    if tableView.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
        tableView.separatorInset = .zero
    }
    
}

Solution 5 - Ios

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

Solution 6 - Ios

In the TableView "Attributes inspector" set the Separator Insets to "Custom" with Left = 0. That is all you have to do!

Solution 7 - Ios

As WTIFS mentioned, UITableViewCell's indentation property is a great way to indent text labels in a built-in cell styles. Just do something like this to achieve nice left margin:

cell.indentationLevel = 2;

This, however, would not work for imageView.

Solution 8 - Ios

Here is the swift version of Chetan's answer:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        tableView.separatorInset = UIEdgeInsets.zero
        tableView.layoutMargins = UIEdgeInsets.zero
        cell.preservesSuperviewLayoutMargins = false
        cell.layoutMargins = UIEdgeInsets.zero
        cell.separatorInset = UIEdgeInsets.zero
}

Solution 9 - Ios

And if you want to change just the left margin of the cell's textlabel only, then change the Horizontal Space Constraint 'Constant' to say 16 or 8 based on the padding you want.(This is in the nib file). If you cant get to the 'constant' Select the label, change the x coordinate in the FrameRectangle View, and then click on the constraint pin at the left) enter image description here

Solution 10 - Ios

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionj bView Question on Stackoverflow
Solution 1 - IosMilanPanchalView Answer on Stackoverflow
Solution 2 - IosFarhan C KView Answer on Stackoverflow
Solution 3 - IosFrancesco VadicamoView Answer on Stackoverflow
Solution 4 - IosChetanView Answer on Stackoverflow
Solution 5 - IosAlessandro FrancucciView Answer on Stackoverflow
Solution 6 - IosNicolai NitaView Answer on Stackoverflow
Solution 7 - IosDannie PView Answer on Stackoverflow
Solution 8 - IosPaul LehnView Answer on Stackoverflow
Solution 9 - IosNaishtaView Answer on Stackoverflow
Solution 10 - IosWTIFSView Answer on Stackoverflow