【问题标题】:How to set UITableViewCell to the full width of screen?如何将 UITableViewCell 设置为屏幕的全宽?
【发布时间】:2017-02-06 09:27:23
【问题描述】:

说明:

如下图所示,这里是一个自定义的 UITableViewCell ,它被聚焦。

当聚焦时,我将其 contentView 的背景颜色设置为黑色,如下所示:

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

此外,当我通过 StoryBoard 检查 contentView 的宽度时,它清楚地显示“1904”而不是预期的“1920”。灰显,无法修改。

此时很明显,contentView 并没有占据屏幕的整个宽度。

我尝试过的:

我经历了多种不同的可能性。

我一开始以为是和tableView的contentInset有关:

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

然后我以为是tableCell的布局边距:

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

然后我尝试更改 UITableCell 的 contentView 框架:

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

我终于尝试了某种形式的self.frameself.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)

问题:

如何将UITableViewCell设置为全屏?

截图:

https://i.stack.imgur.com/mAoUh.png

显示我的 tableviewcell 的 contentView 边距的图像(它们与 tableviewcells 相同)

https://i.stack.imgur.com/20X74.png

图片展示了如何为我的 tableView 添加对齐约束。

(帖子太大,链接原因)

【问题讨论】:

  • 你能显示你的tableView的约束吗?您是否检查过是否已将其设置为您的超级视图的边缘?边距通常为 8pt,所以在两边,它可以解释你所期望的和你得到的之间 16pt 的差异。
  • 有没有在TableView的cell类中尝试过self.layoutIfNeeded
  • 在你的tableview的单元类中试试
  • 您能否在控制器中(在故事板中)选择 tableView 的左约束或右约束,并在第一项或第二项是 SuperView.Trailing/Leading Margin 时检查大小检查器?我不是超级你将边距设置为显式 0 就足够了
  • @Ocunidee,将 TableViewController 更改为 ViewController 解决了我的问题。谢谢。您可以将其添加为答案。我觉得 TableViewController 这样做很奇怪。

标签: swift uitableview swift3 tvos


【解决方案1】:

鉴于您正在使用 UITableViewController,您几乎无法控制其行为,也无法控制控制器内 tableView 的约束。 我建议您在 UIViewController 中切换 tableView,注意将约束设置为超级视图的前导和尾随,而不是其边距。

【讨论】:

    猜你喜欢
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    相关资源
    最近更新 更多