I have table view. In it I have section, each section has one row only. I use the sections so that I can control the space between the rows with header height. I would like to add padding to UITableView
so that I can space the cells/sections
from the edges of the device. The spacing needs to have the color of the UITableView
background.
Asked
Active
Viewed 7,278 times
6

Atanu Mondal
- 1,714
- 1
- 24
- 30

h3dkandi
- 1,106
- 1
- 12
- 27
2 Answers
10
Subclass UITableviewCell
and override setFrame
method of it like,
- (void)setFrame:(CGRect)frame {
frame.origin.x += inset;
frame.size.width -= 2 * inset;
[super setFrame:frame];
}
So, it will make padding of two pixel from left and right side. you can change it as per requirement.
You can use contentInset
but it is work only for vertical spacing (top and bottom). You should setFrame of cell as mentioned above for horizontal spacing.
You can take this answer and this answer as a reference.
Hope this will help :)

Community
- 1
- 1

Ketan Parmar
- 27,092
- 9
- 50
- 75
-
Why contentInset doesn't work for horizontal spacing? – ShadeToD Dec 16 '20 at 13:14
10
As Lion has been mention! His method is working fine but it's in objectiveC. Here is the method for swift 4.2
override var frame: CGRect {
get {
return super.frame
}
set (newFrame) {
var frame = newFrame
frame.origin.x += 10
frame.size.width -= 2 * 10
super.frame = frame
}
}

Anonymous-E
- 827
- 11
- 29
-
When cell in editing mode it will slide back to the wrong position. Any solution? – frank61003 Dec 30 '20 at 09:36