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:
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.
This results in a corresponding margin between the device's screen edge and the UILabel
text at runtime:
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:
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.
Just add the method in you code as mentioned in the below link
The method I would like to mention is
Starting from iOS 8 is available the cell property
layoutMargins
. So the correct way to adjust cell margins is setting this property in yourtableView:cellForRowAtIndexPath
or in your customUITableViewCell
in this way:I hope this can help someone.
In the TableView "Attributes inspector" set the Separator Insets to "Custom" with Left = 0. That is all you have to do!
As WTIFS mentioned,
UITableViewCell
'sindentation
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:This, however, would not work for imageView.
Here is the swift version of Chetan's answer:
And if you want to change just the left margin of the cell's textlabel only, then change the![enter image description here](https://i.stack.imgur.com/j96iN.png)
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)