17

In order to hide a subview in UIStackView is it better to set the isHidden to true or to use removeArrangedSubview and remove the subview from the parent Stackview instead ?

I am using a Stackview to arrange my UIElements in the tableView cell. I currently have a parent StackView and a childStackview arranged inside. The child view needs to be shown or hidden based on a condition. I am setting the isHidden property of the child view to true when the condition turns true.

When I scroll and new cells come become visible I get the following messages in the console. The app does not crash.

NSLayoutConstraint:0x600000093470 'UISV-canvas-connection' UIStackView:0x7fd4527201b0.top == UILabel:0x7fd452720370'Day Off - Rest and Sleep ...'.top (active)

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want.
Try this:

  1. look at each constraint and try to figure out which you don't expect;
  2. find the code that added the unwanted constraint or constraints and fix it. ( "", "", "", "", "", "", "" )

Will attempt to recover by breaking constraint

aircraft
  • 25,146
  • 28
  • 91
  • 166
jay
  • 182
  • 1
  • 1
  • 7
  • 1
    Like most questions about which approach is better, the answer is going to depend on your particular situation. Can you provide a few more details? – Jim Jan 16 '17 at 23:50
  • Thank you Jim, I have added One a ParentStackView and a ChildStackview within one another. I have a need to hide the childstackview based on a condition. When I set the isHidden of the child stackview to true, i get the following message in the console log of xcode. It does not crash the app though. I get this error Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Will attempt to recover by breaking constraint – jay Jan 17 '17 at 00:00
  • Jay, go ahead and edit your question and add that detail there before people downvote it for not having any detail. Also, let's say the child stack view goes away (you hide it or use removeArrangedSubview). Do you ever want it to come back? – Jim Jan 17 '17 at 00:04
  • 1
    Thank you for helping me Jim, :) this is my first question so learning how to ask it so the community has all the information to respond. no Jim i dont have a need for it to come back once hidden. – jay Jan 17 '17 at 00:15

1 Answers1

17

To answer your first question, if you don't have a need to unhide the subview, the most logical thing to do would be to remove it using removeArrangedSubview(UIView). As you might know, the stack view will automagically update its layout whenever views are added, removed, inserted, or hidden/unhidden.

The warning you're getting in the console about the constraints may or may not be related to whatever you've implemented for the subview right now. Did you mention it because you think it might be related?

Hope that helps.

Jim
  • 1,260
  • 15
  • 37
  • Thank you Jim, it helps to get a conceptual clarity. I thought they were related as I started seeing these messages in console after I implemented the stackview. – jay Jan 17 '17 at 00:48
  • 1
    You're welcome, Jay. Since you're new to the question asking thing around here (welcome, by the way), go ahead and mark my answer as the correct one if it helped you and you're satisfied with it. If you continue to have trouble with the conflicting constraints, post another question and remember to include the important details. Those constraint problems can be really tricky to resolve. – Jim Jan 17 '17 at 00:53
  • done!, i have marked the answer. And the constraint errors were appearing because of an image not being in the imageview. Once I fixed that constraint errors also disappeared. – jay Jan 17 '17 at 10:27