1

I know this is a known issue and has been discussed a lot. However, I've been over a lot of these questions and none of these solutions solve my problem. Also, I work completely in code so no storyboard.

What I have read so far:

My case is the following. I am using a UITableView with sections. Every section has a headerView and footerView. Above this, the entire table also has a headerView and a footerView. So there is one general header and footer, and a header and footer for each section. The header for the entire UITableView shows a title, the footer shows a "Add Section" and "Post" button. The header for a section shows the title of the section, and the footer for a section shows a "Add Row" button.

I do NOT set estimated heights. I used to, but I removed them as I didn't need estimates, I knew the value it would be (it's not dynamic) - however, removing them or adding them doesn't seem to influence my problem. So there is no estimatedHeightForFooterInSection, estimatedHeightForHeaderInSection or estimatedHeightForRowAt. I add constraints as they should and everything works fine, upon opening the UIViewController with the UITableView, there are no errors. However, the Encapsulated-Height or Encapsulated-Width error shows up when I insert a new section (it doesn't happen when I insert a new row):

"<NSLayoutConstraint:0x7ff8557ce1a0 H:|-(15.9574)-[Project.ShadowView:0x7ff8557c8ba0]   (active, names: '|':_TtCC5Project26TableViewController14sectionFooter:0x7ff8557c88e0 )>",
"<NSLayoutConstraint:0x7ff8557ce660 Project.ShadowView:0x7ff8557c8ba0.trailing == _TtCC5Project26TableViewController14sectionFooter:0x7ff8557c88e0.trailing - 15.9574   (active)>",
"<NSLayoutConstraint:0x7ff851ea1510 'UIView-Encapsulated-Layout-Width' _TtCC5Project26TableViewController14sectionFooter:0x7ff8557c88e0.width == 0   (active)>"

It also happens, every now and then, with the height instead of width (.height == 0). ShadowView refers to my footerView (which is a custom view with shadow behind it). The constraints for this view are (normalSpacing is a variable determined based on the screen width):

footerView.topAnchor.constraint(equalTo: topAnchor).isActive = true
footerView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -normalSpacing).isActive = true
footerView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: normalSpacing).isActive = true
footerView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -normalSpacing).isActive = true

Important: my views are not broken, everything works as expected, the error only shows up in my console and makes me want to fix it. So what is this, where does it come from and above all: how do I fix it?

Shamas S
  • 7,507
  • 10
  • 46
  • 58
PennyWise
  • 595
  • 2
  • 12
  • 37
  • In the footerView Datasource method, create a view add the footerView as subview and return that view as Footer View. – Sachin Vas Mar 08 '19 at 08:36
  • I have now added a UIView "footerContainer" with `translatesAutoresizingMaskIntoConstraints` set to false. How should I set the constraints on this container? When I set them up the same as footerView, it doesn't change anything and the error remains. Then tried to pin the footerContainer to the edges of footerView, also unsuccessful in removing the error. – PennyWise Mar 08 '19 at 08:50
  • Don't provide any constraints to the footerContainer, just create an empty view and add the subview and return the view. – Sachin Vas Mar 08 '19 at 08:53
  • Did just that. Added the footerContainer as subview and added my other views to footerContainer, without setting any constraints on footerContainer and keeping the other ones as they were. Doesn't fix the error. – PennyWise Mar 08 '19 at 08:59
  • Can you upload your code? – Sachin Vas Mar 08 '19 at 09:00
  • Did it here: https://pastebin.com/hwPLypdi (without your changes though as they did not affect the error - this is how I set up the footerView) – PennyWise Mar 08 '19 at 09:13
  • Also, can you set footerContainer.autoresizingMask = [.flexibleWidth] and check? – Sachin Vas Mar 08 '19 at 09:43
  • Doesn't work. In all honesty; how bad would it be if I leave it just as-is? I feel like I've tried about everything there is to try and losing days over just researching this issue. Would it be a big deal to leave this error in my app, because everything works as expected so I don't think it would be such a problem? – PennyWise Mar 08 '19 at 10:02
  • I know its too frustrating to fix the layout issues. let constaint = footerView.widthAnchor.constraintEqualToAnchor(tableView.widthAnchor) constraint.priority = 999, constraint.active = true. Can you try with this? – Sachin Vas Mar 08 '19 at 10:13
  • Had indeed read about this "fix" as well, but it doesn't work for me.. I must be doing something, somwhere that enforces the "0" height or "0" width, but I have no idea where it originates from. – PennyWise Mar 08 '19 at 10:29

0 Answers0