skip to Main Content

I want to add spacing between multiple custom cells. I have multiple sections that contains the cells.

I have a ViewController class for the UITableView where I specify the information:

struct customCell {
        let title: String
} 

 func configure() {
            models.append(Section(title: "", options: [
             customCell(title: "Sounds"){
            },
}

And I have a different UITableViewCell class where I use the layoutSubViews() method:

override func layoutSubviews() {
        super.layoutSubviews()
      
        contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0))

I see other solutions on this website, where they get the spacing by using UIEdgeInsets.

I tried using the layoutSubViews() method but that didn’t work, it pushes the content of the cell upwards instead of adding spaces between cells:

example

When I use all insets:

contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top:15, left: 15, bottom: 15, right: 15))

The content gets pushed inwards from all sides:

enter image description here

What am I doing wrong?

2

Answers


  1. Try this:

    override func layoutSubviews() {
        super.layoutSubviews()
    contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
    }
    

    And the other solution can be that if you use UIView in your contentView and declare other objects in that View, it will behave like you add space between cell.

    Login or Signup to reply.
  2. Try to set nagative value for inset, like this:

    override func layoutSubviews() {
        super.layoutSubviews()
        contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 0, left: 0, bottom: -10, right: 0))
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search