简体   繁体   English

UITableView左右填充

[英]UITableView left and right padding

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. 我想为UITableView添加填充,以便我可以从设备的边缘UITableView cells/sections The spacing needs to have the color of the UITableView background. 间距需要具有UITableView背景的颜色。

Subclass UITableviewCell and override setFrame method of it like, 子类UITableviewCell并覆盖它的setFrame方法,如,

- (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). 您可以使用contentInset但它仅适用于垂直间距(顶部和底部)。 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 :) 希望这会有所帮助:)

As Lion has been mention! 狮子已被提及! His method is working fine but it's in objectiveC. 他的方法工作正常,但它在ObjectiveC中。 Here is the method for swift 4.2 这是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
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM