Swift tableHeaderView 自动布局问题

Swift tableHeaderView autolayout issue

我已经被这个问题困扰了一段时间了。 我正在使用 自定义视图 (CollectionView) 作为 tableHeaderView (蓝色矩形),我想在使用自动布局时添加一些边距,但没有似乎有效

我试过了

  NSLayoutConstraint(item: homeCarousel, attribute: .topMargin, 
     relatedBy: .equal, toItem: self, attribute: .topMargin, 
     multiplier: 1.0, constant: 20).isActive = true
 ------
  homeCarousel.topAnchor.cons.....

您不能直接向 UITableView 的 header 视图添加边距。使用简单的 UIView 作为容器(紫色视图)并将 CollectionView(绿色视图)嵌套在其中。然后您将能够使用故事板或代码设置约束。

See screenshot here