0

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.

grid

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)>"

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

TextField1's width should equal 45.

StackView's width should equal 0.

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

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

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.

Amber K
  • 683
  • 1
  • 5
  • 15
DrWhat
  • 1,858
  • 3
  • 14
  • 28
  • Why not use `uicollectonview` ? – Amber K Apr 17 '19 at 09:28
  • Do you mean to put rows of UICollectionViews into the TableViewCell? I could try this I guess, but could imagine this might add more complexity. Without understanding my problem, I might end up in the same position. – DrWhat Apr 17 '19 at 11:10
  • Why tableview, uicollectionview will do the job easily including headers. I guess you can sacrifice a little complexity as an investment, it will be organized, easier to debug and customize. – Amber K Apr 17 '19 at 11:32
  • For a collection views - is it necessary to create a new collection view for each row? Would it be feasible with static content, like https://robkerr.com/how-to-create-a-static-uicollectionview/? Is it necessary to define the size of each cell, or can cells be sized based on their intrinsic content size? Is it a case of setting up data source and delegate methods to define content sizes and what else for all cells, like https://stackoverflow.com/questions/28325277/how-to-set-cell-spacing-and-uicollectionview-uicollectionviewflowlayout-size-r? – DrWhat Apr 18 '19 at 14:59

2 Answers2

0

It looks like UITextField width is set as 45 as constraint in your storyboard.

Add Symbolic constraint for UIViewAlertForUnsatisfiableConstraints in XCode and try debugging the issue.

enter image description here

Anand
  • 1,519
  • 2
  • 15
  • 21
  • I've done this, but cannot work out what this tells me. The program stops before navigating to the new view. There is nothing in the log. This is the line where it breaks: 0x10dec22f6 : pushq %rbp – DrWhat Apr 17 '19 at 11:02
0

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)
}
DrWhat
  • 1,858
  • 3
  • 14
  • 28