简体   繁体   English

UIStackView 布局边距内的 UITableView

[英]UITableView inside UIStackView layout margins

I currently have a UITableView embedded inside a UIStackView .我目前有一个嵌入在UITableView中的UIStackView I've currently set the StackView to have its own padding like so:我目前已将 StackView 设置为具有自己的填充,如下所示:

stackView.layoutMargins = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
stackView.isLayoutMarginsRelativeArrangement = true

I've also set the stackview to be set on its edges, and it's also under a scrollview.我还将堆栈视图设置在其边缘,并且它也在滚动视图下。

However, applying those settings, I'm getting errors on the UITableView constraints, telling me that the layout margins constraints are the issue.但是,应用这些设置,我在 UITableView 约束上遇到错误,告诉我布局边距约束是问题所在。

(
    "<NSLayoutConstraint:0x600000645f90 UIStackView:0x7fa9bbc32260.width == UIScrollView:0x7fa9bc019800.width   (active)>",
    "<NSLayoutConstraint:0x6000006441e0 H:|-(0)-[UIScrollView:0x7fa9bc019800](LTR)   (active, names: '|':COVID_19_Compliance_Coach.DashboardView:0x7fa9bbc11340 )>",
    "<NSLayoutConstraint:0x600000644320 UIScrollView:0x7fa9bc019800.right == COVID_19_Compliance_Coach.DashboardView:0x7fa9bbc11340.right   (active)>",
    "<NSLayoutConstraint:0x600000646f30 '_UITemporaryLayoutWidth' COVID_19_Compliance_Coach.DashboardView:0x7fa9bbc11340.width == 0   (active)>",
    "<NSLayoutConstraint:0x600000647250 'UISV-canvas-connection' UILayoutGuide:0x600001c76a00'UIViewLayoutMarginsGuide'.leading == UITableView:0x7fa9bc0a2e00.leading   (active)>",
    "<NSLayoutConstraint:0x6000006472f0 'UISV-canvas-connection' UILayoutGuide:0x600001c76a00'UIViewLayoutMarginsGuide'.trailing == UITableView:0x7fa9bc0a2e00.trailing   (active)>",
    "<NSLayoutConstraint:0x6000006470c0 'UIView-leftMargin-guide-constraint' H:|-(8)-[UILayoutGuide:0x600001c76a00'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':UIStackView:0x7fa9bbc32260 )>",
    "<NSLayoutConstraint:0x600000647160 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600001c76a00'UIViewLayoutMarginsGuide']-(8)-|(LTR)   (active, names: '|':UIStackView:0x7fa9bbc32260 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000006472f0 'UISV-canvas-connection' UILayoutGuide:0x600001c76a00'UIViewLayoutMarginsGuide'.trailing == UITableView:0x7fa9bc0a2e00.trailing   (active)>

I can't seem to figure out what the error is.我似乎无法弄清楚错误是什么。 If someone can point me to the right direction, that'd be extremely helpful, or if there's a different approach, that'd be helpful too.如果有人能指出我正确的方向,那将非常有帮助,或者如果有不同的方法,那也会有帮助。

Here's also a guideline of what the view hierarchy looks like:这也是视图层次结构的指南:

UIScrollView
 -> UIStackView
     -> UITableView

I would strongly suggest that you don't add a table view inside a scroll view since that table view already has a scroll view.我强烈建议您不要在滚动视图中添加表格视图,因为该表格视图已经具有滚动视图。

That being said I would add the constraints this way话虽如此,我会以这种方式添加约束

  1. ScrollView:滚动视图:

    • Pin it to all corners of the superview将其固定到超级视图的各个角落
  2. Stack View:堆栈视图:

    • Pin it to the four corners of the the scroll view将其固定到滚动视图的四个角
    • Create a height constraint that is equal to the scroll view height创建一个等于滚动视图高度的高度约束
    • Create a width constraint that is equal to the scroll view width创建一个等于滚动视图宽度的宽度约束
  3. Table View表视图

    • Add the table view to the stack view将表视图添加到堆栈视图

在此处输入图像描述

If you need to add a height constraint to the the table view you need to remove the height constraint of the stack view如果您需要向表视图添加高度约束,则需要删除堆栈视图的高度约束

在此处输入图像描述

If you add additional views to the stack view you will need to add a height constraint to them as well如果您向堆栈视图添加其他视图,则还需要向它们添加高度约束

I realized that I was overthinking on how the designs should be implemented.我意识到我在设计应该如何实施上想得太多了。 I've removed the UIStackView , and replaced it with a UITableView as the root instead.我已删除UIStackView ,并将其替换为UITableView作为根。

So now the Hierarchy is more:所以现在层次结构更多:

UITableView
 -> SectionHeaderView 
 -> CustomCells (dequeuedReusable)

I decided to put those extra views onto the section header view at the top, and then used the cells instead.我决定将这些额外的视图放在顶部的 header 视图部分,然后改用单元格。 Thank you guys for the suggestion and clearer approach.谢谢你们的建议和更清晰的方法。 Much appreciated.非常感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UITableView 未显示在 UIStackView 中 - UITableView not shown inside UIStackView UIViewController里面的UITableView意外边距 - UITableView inside UIViewControllerView unexpected margins 在UITableView内以编程方式将UIViews添加到UIStackView - Add UIViews to UIStackView programmatically inside UITableView UIStackView 高度约束问题内的 UITableView - UITableView inside a UIStackView height constraint issue 有什么办法可以改变UIStackView中的UITextView的行距/边距/填充? - Is there any way to alter line spacing/margins/padding for a UITextView inside a UIStackView? 内置UIImageView的UIStackView上的自动布局 - iOS Swift - Auto Layout on UIStackView with UIImageView inside - iOS Swift UIStoryBoard Auto Layout在UIScrollView内嵌入UIStackView - UIStoryBoard Auto Layout embed UIStackView inside UIScrollView 如何在UIStackView中将UITableView保持在X和Y中心 - How to keep UITableView centered on X and Y inside an UIStackView 为什么单视图的UIStackView,按比例填充,布局边距会导致模糊约束错误? - Why does a UIStackView with a single view, fill Proportionally, and Layout Margins causes ambiguous constraint error? 为什么嵌入的 UIStackView,按比例填充,间距和布局边距会导致约束错误以及如何修复? - Why do embedded UIStackView's, filled Proportionally, with spacing and Layout Margins causes constraint errors and how to fix?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM