Search code examples
swiftuitableviewswift3tvos

How to set UITableViewCell to the full width of screen?


Description:

As can be seen in the picture below, here is a custom UITableViewCell that is focused.

width not full

When focused I set its contentView's background color to black, like so:

//Where next is the next cell being focused.
next.contentView.backgroundColor = UIColor.black

Additionally, when I check the contentView's width via the StoryBoard it clearly says "1904" instead of the expected "1920". It's grayed out and I can not modify it.

It's clear at this point that the contentView does not take up the full width of the screen.

What I've tried:

I went through multiple different possibilities.

I first thought it was something to do with the tableView's contentInset:

//Inside my tableView controller
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)

Then I thought it was the tableCell's layout margin:

//Inside my custom UITableCell
self.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)

Then I tried changing the UITableCell's contentView frame:

//Inside my custom UITableCell
self.contentView.frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(0, 0, 0, 0))

I finally tried some form of self.frame and self.contentView.frame:

//In my UITableCell
self.contentView.frame = CGRect.init(x: self.bounds.maxX, y: self.bounds.maxY, width: self.bounds.width + 100, height: self.bounds.height)

Question:

How to set UITableViewCell to the full width of screen?

Screenshots:

https://i.sstatic.net/mAoUh.png

Image showing my tableviewcell's contentView's margins (they are the same as the tableviewcells)

https://i.sstatic.net/20X74.png

Image showing how I can add align constraints for my tableView.

(too big for post, reason for link)


Solution

  • Given you are working with a UITableViewController, you have very little control about its behavior and none about the constraints of the tableView within the controller. I suggest you switch a tableView within a UIViewController minding setting your constraints to the leading and trailing of the superview, not its margins.