1

I have a dynamic table view in grouped style and different custom cells in separate nibs, which then I register in viewDidLoad and add them to the tableview accordingly. In story board I added a uiview under the prototype cell to be my footer inside there's a button. Now because my cells have borders I want to have a left and right margin , but not for the whole tableView , only for the cells . I did use this code :

self.tableView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);

but doing this , my footer doesn't have the width of the screen anymore.

I looked in the web and found some people use this:

import UIKit

class MyTableViewCell: UITableViewCell {

    override func layoutSubviews() {
        super.layoutSubviews()
        contentView.frame = UIEdgeInsetsInsetRect(contentView.frame, UIEdgeInsetsMake(0, 16, 0, 16))
    }
}

but this doesn't do anything.

Note that viewForHeaderinSection and viewForFooterInSection are used on my application. Any suggestions please. Thank you

10
  • you can use method for set contentInset. Commented Jun 25, 2018 at 13:50
  • import UIKit class MyTableViewCell: UITableViewCell { override func layoutSubviews() { super.layoutSubviews() contentView.frame = UIEdgeInsetsInsetRect(contentView.frame, UIEdgeInsetsMake(0, 16, 0, 16)) } } you won’t see the applied margin because under the content view there is a background view of the table cell. So it prevents the margin because it is by default white, so it should be transparent Commented Jun 25, 2018 at 13:51
  • try setting the background color of cell to transparent(clear color) Commented Jun 25, 2018 at 13:54
  • @ChiragKothiya you mean like this : self.tableView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10) . I did do that but as I said the margin is also applied to the footer and I don't want that Commented Jun 25, 2018 at 13:55
  • @Vinaykrishnan I did that still nothing happens :( Commented Jun 25, 2018 at 13:57

1 Answer 1

2

For future reference this is how I fixed it :

override var frame: CGRect {
    get {
        return super.frame
    }
    set {
        var frame = newValue
        frame.origin.x += 25
        frame.size.width -= 2 * 25

        super.frame = frame
    }
}

thanks to https://stackoverflow.com/a/42094806

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.