[英]How to set UITableViewCell to the full width of screen?
As can be seen in the picture below, here is a custom UITableViewCell that is focused. 如下图所示,这是一个自定义的UITableViewCell。
When focused I set its contentView's background color to black, like so: 聚焦后,将其contentView的背景色设置为黑色,如下所示:
//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". 另外,当我通过StoryBoard检查contentView的宽度时,它会清楚地显示“ 1904”而不是预期的“ 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. 在这一点上很明显,contentView不会占据屏幕的整个宽度。
I went through multiple different possibilities. 我经历了多种不同的可能性。
I first thought it was something to do with the tableView's contentInset: 我首先认为这与tableView的contentInset有关:
//Inside my tableView controller
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
Then I thought it was the tableCell's layout margin: 然后我认为这是tableCell的布局边距:
//Inside my custom UITableCell
self.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
Then I tried changing the UITableCell's contentView frame: 然后,我尝试更改UITableCell的contentView框架:
//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
: 我终于尝试了某种形式的self.frame
和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)
How to set UITableViewCell
to the full width of screen? 如何将UITableViewCell
设置为屏幕的整个宽度?
https://i.stack.imgur.com/mAoUh.png https://i.stack.imgur.com/mAoUh.png
Image showing my tableviewcell's contentView's margins (they are the same as the tableviewcells) 该图显示了tableviewcell的contentView的边距(它们与tableviewcell相同)
https://i.stack.imgur.com/20X74.png https://i.stack.imgur.com/20X74.png
Image showing how I can add align constraints for my tableView. 该图显示了如何为tableView添加对齐约束。
(too big for post, reason for link) (对于发布而言太大,导致链接的原因)
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. 鉴于您正在使用UITableViewController,因此对其行为的控制几乎没有,而对控制器内tableView的约束没有任何控制。 I suggest you switch a tableView within a UIViewController minding setting your constraints to the leading and trailing of the superview, not its margins. 我建议您在UIViewController思维中切换tableView,将约束设置为超级视图的开头和结尾,而不是其边距。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.