本文介绍了从日志输出中识别和修复自动布局问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎在界面构建器情节提要中准备了一款专注于多设备信息的iOS应用.我正在将带有文本或其他内容的表视图控制器用于静态表视图单元格中.

I'm preparing a multi device information focused iOS app almost exclusively in interface builder storyboard. I'm using table view controllers with text or other content into static table view cells.

在特定的单元格中,我想要一个具有相同高度行但宽度不同的文本字段网格-取决于文本字段的长度.

In specific cells, I want a grid of text fields with the same height rows but various widths - depending on the length of the text field.

我正在使用包含水平堆栈视图的垂直堆栈视图,所有这些视图均具有对齐":填充"和分布":按比例填充".

I'm using a vertical stack view that contains horizontal stack views, all with Alignment: Fill and Distribution: Fill Proportionally.

如果我不减小最小字体大小,则网格在某些设备上根本不会出现,但是经过很多摆弄之后,我可以使网格以类大小出现在模拟器上.

If I do not reduce the minimum font size, the grid does not appear at all on certain devices, but after a lot of fiddling, I can make the grid appear on the simulator across class sizes...

但是,日志显示无法同时满足约束条件..."

BUT, the log reveals "Unable to simultaneously satisfy constraints..."

"<NSLayoutConstraint:0x600002bde620 UITextField:0x7f92608dc200.width == 45   (active)>",
"<NSLayoutConstraint:0x600002bfe210 'fittingSizeHTarget' UIStackView:0x7f925f463d70.width == 0   (active)>",
"<NSLayoutConstraint:0x600002bd2cb0 'UISV-canvas-connection' UIStackView:0x7f925f463d70.leading == UITextField:0x7f92608dc200.leading   (active)>",
"<NSLayoutConstraint:0x600002bd2d00 'UISV-canvas-connection' H:[UITextField:0x7f926081b200]-(0)-|   (active, names: '|':UIStackView:0x7f925f463d70 )>",
"<NSLayoutConstraint:0x600002bd4be0 'UISV-spacing' H:[UITextField:0x7f92608dc200]-(0)-[UITextField:0x7f926081b200]   (active)>"

我从 https://www.wtfautolayout.com 理解的

意思是:

which I understand from https://www.wtfautolayout.com means:

TextField1的宽度应等于45.

TextField1's width should equal 45.

StackView的宽度应等于0.

StackView's width should equal 0.

StackView的前端应等于TextField1的前端.‡

StackView's leading edge should equal TextField1's leading edge.‡

StackView的后沿应等于TextField2的后沿.‡

StackView's trailing edge should equal TextField2's trailing edge.‡

TextField2的前缘应等于TextField1的后缘.‡

TextField2's leading edge should equal TextField1's trailing edge.‡

后三个看起来很熟悉,但是我没有安装,也找不到宽度限制(我想这是基于内容的.)

The latter three look familiar, but I did not install and cannot find the width constraints (which I guess are content based).

该日志随后报告说,打破上面列表中的第一个约束似乎是解决方案,但是随之而来的是另一个非常相似的日志错误,其值略有不同-可能是网格中的下一行.

The log then reports that breaking the first constraint in the list above seems to be the solution, but another very similar log error follows with slightly different values - probably the next row in the grid.

我的问题是确切确定发生的位置,发生的原因以及如何有效地解决它.

My problem is in identifying exactly where this is happening, why it is happening, and how do I effectively fix it.

推荐答案

在发表评论之后,我尝试使用UICollectionView创建类似于表格的表格.下面的代码参考源代码.与安排静态内容和依靠自动布局相比,这是很多工作,但是如果自动布局无法提供此布局,则我必须接受此作为答案.

Following a comment, I have tried using UICollectionView to create a grid like table. Code below with reference to source. This is a lot of work compared to arranging static content and relying on auto layout, but if auto layout cannot deliver this layout then I have to accept this as the answer.

即使使用这种方法,网格中的最后一行也有问题-.但是,如果我可以使最后一行对齐,并且没有人建议使用约束可以建立网格,那么我将更新并接受此答案是正确的.

Even using this approach, I have a problem with the final row in the grid - Collection view grid last row appears differently despite equal widths - swift iOS . Nevertheless, if I can get the last row to align, and no one suggests that a grid is possible using constraints, then I will update and accept this answer to be correct.

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
    let settings = currentContents[indexPath.item]
    let height = CGFloat(30)

    // https://stackoverflow.com/questions/54915227/uicollectionview-remove-space-between-cells-with-7-items-per-row
    var cellWidth = CGFloat()
    let availableWidth = collectionView.bounds.size.width
    print("available width", availableWidth)
    let minimumWidth = floor(availableWidth / collectionContents.cellsPerRow5)
    // let remainder = availableWidth - minimumWidth * CGFloat(cellsPerRow4)
    print("minmum width", minimumWidth)
    cellWidth = minimumWidth * settings.portion - 1
    print("cell width", cellWidth)

    return CGSize(width: cellWidth, height: height)
}

这篇关于从日志输出中识别和修复自动布局问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:13