【问题标题】:Change the layout margins of a UITableViewController programmatically以编程方式更改 UITableViewController 的布局边距
【发布时间】:2021-08-04 07:53:58
【问题描述】:

第一张图片是我的细胞目前的样子,第二张图片是我想要的样子。我有一个 UITableViewController 并想以编程方式更改布局边距,但它不起作用

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
}

【问题讨论】:

  • 你应该把所有的项目放在一个UIVIew() 然后给这个视图留出边距给单元格内容视图。
  • @Kudos 这些项目已经在 contentView 中,但是当 tableView 加载时对 layoutMargins 的更改不起作用
  • 没有。我的意思是所有项目都应该在另一个视图中,并且该视图在 ContentView 中。

标签: ios swift uikit tableview


【解决方案1】:

在单元格内放置一个 UIView 作为背景视图,并通过在情节提要中调整其大小来为该视图提供边距。然后将单元格内容视图的颜色不透明度更改为0

【讨论】:

  • 所有标签和图像都在contentView 中,因此我可以更改可行的帧大小。所以我只是忽略了 layoutMargins 属性?
  • 您需要在 contentView 中添加一个新视图,然后添加所有标签,并且应该在新视图中添加图像。我们可以改变新视图的框架。
猜你喜欢
  • 1970-01-01
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
  • 2012-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多