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

后端 未结 10 1407
天涯浪人
天涯浪人 2020-12-02 14:27

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

相关标签:
10条回答
  • 2020-12-02 14:33

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

    0 讨论(0)
  • 2020-12-02 14:39

    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

    0 讨论(0)
  • 2020-12-02 14:44

    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)

    0 讨论(0)
  • 2020-12-02 14:48

    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

    0 讨论(0)
  • 2020-12-02 14:50

    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.

    0 讨论(0)
  • 2020-12-02 14:51

    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.

    0 讨论(0)
提交回复
热议问题