Search code examples
iosswiftuikittableview

Change the layout margins of a UITableViewController programmatically


The first image is how my cells are currently and the second image is how I want them to be. I have a UITableViewController and want to programmatically change the layout margins however it does not work

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor(named: "Gray")
    tableView.backgroundColor = UIColor(named: "Gray")
    tableView.contentInset.top = .padding
    tableView.separatorStyle = .none
    tableView.register(TaskCell.self, forCellReuseIdentifier: "taskCell")
    tableView.layoutMargins = .init(top: 0, left: 20, bottom: 0, right: 20) // Does not work
}

enter image description here enter image description here


Solution

  • Put a UIView inside the cell as background view and give margins to that view by adjusting its size in the storyboard. Then change the colour opacity of the cell's content view to 0

    enter image description here