Kilom94l
Kilom94l

Reputation: 85

iOS - Left and right margin for UITableViewCells in Swift

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

Upvotes: 1

Views: 1764

Answers (1)

Kilom94l
Kilom94l

Reputation: 85

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

Upvotes: 2

Related Questions