UIStackView - layout constraint issues when hiding stack views

38,994

Solution 1

Have you tried this? Calling super after your changes?

override func viewWillAppear() {
    self.nameLabel.text = "John"
    self.summaryStackView.hidden = true
    self.combinedStackView.hidden = true
    super.viewWillAppear()
}

Solution 2

I had the same problem and I fixed it by giving the height constraints of my initially hidden views a priority of 999.

enter image description here

The problem is that your stackview applies a height constraint of 0 on your hidden view which conflicts with your other height constraint. This was the error message:

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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7fa3a5004310 V:[App.DummyView:0x7fa3a5003fd0(40)]>",
    "<NSLayoutConstraint:0x7fa3a3e44190 'UISV-hiding' V:[App.DummyView:0x7fa3a5003fd0(0)]>"
)

Giving your height constraint a lower priority solves this problem.

Solution 3

This is a known problem with hiding nested stack views.

There are essentially 3 solutions to this problem:

  1. Change the spacing to 0, but then you'll need to remember the previous spacing value.
  2. Call innerStackView.removeFromSuperview(), but then you'll need to remember where to insert the stack view.
  3. Wrap the stack view in a UIView with at least one 999 constraint. E.g. Top, Leading, Trailing @ 1000, Bottom@999.

The 3rd option is the best in my opinion. For more information about this problem, why it happens, the different solutions, and how to implement solution 3, see my answer to a similar question.

Solution 4

You can use the removeArrangedSubview and removeFromSuperview property of UIStackView.

In Objective-C :

 [self.topStackView removeArrangedSubview:self.summaryStackView];
 [self.summaryStackView removeFromSuperview];

 [self.topStackView removeArrangedSubview:self.combinedStackView];
 [self.combinedStackView removeFromSuperview];

From Apple UIStackView Documentation:(https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIStackView_Class_Reference/#//apple_ref/occ/instm/UIStackView/removeArrangedSubview:)

The stack view automatically updates its layout whenever views are added, removed or inserted into the arrangedSubviews array.

  • removeArrangedSubview: This method removes the provided view from the stack’s arrangedSubviews array. The view’s position and size will no longer be managed by the stack view. However, this method does not remove the provided view from the stack’s subviews array; therefore, the view is still displayed as part of the view hierarchy.

To prevent the view from appearing on screen after calling the stack’s removeArrangedSubview: method, explicitly remove the view from the subviews array by calling the view’s removeFromSuperview method, or set the view’s hidden property to YES.

Solution 5

When the UIViewStack is hidden, the constraints automatically generated by the UIStackView will throw lots of UISV-hiding, UISV-spacing, UISV-canvas-connection warnings, if the UIStackView's spacing property has any value other than zero.

This doesn't make much sense, it's almost certainly a framework bug. The workaround I use is to set the spacing to zero when hiding the component.

if hideStackView {
    myStackView.hidden = true
    myStackView.spacing = CGFloat(0)
} else {
    myStackView.hidden = false
    myStackView.spacing = CGFloat(8)
}
Share:
38,994
JEL
Author by

JEL

Updated on August 09, 2020

Comments

  • JEL
    JEL almost 4 years

    My app has 2 screens:

    1. TableViewVC (no stack views here)

    2. DetailVC (all the nested stack views here; please see link for picture: Nested StackViews Picture) -- Note, there are labels and images within these stack views.

    When you press a cell in the tableview, it passes the information from the TableViewVC to the DetailVC. The problem is with hiding the specific UIStackViews in the DetailVC. I want only 2 stack views out of the various ones in the DetailVC to be hidden as soon as the view loads. So I write this code in the DetailVC to accomplish this:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.nameLabel.text = "John"
    
        self.summaryStackView.hidden = true
        self.combinedStackView.hidden = true
    }
    

    Everything looks great but Xcode give many warnings only at runtime. There are no warning in Storyboard when the app is not running. Please see link for picture of errors: Picture of Errors

    Basically it's a lot of UISV-hiding, UISV-spacing, UISV-canvas-connection errors. These errors go away if I hide the same stack views in viewDidAppear but then there is a flash of the stuff that was supposed to be hidden and then it hides. The user sees the the view briefly and then it hides which is not good.

    Sorry for not being able to actually post pictures instead of links, still can't do so.

    Any suggestions on how to fix this? This is for an app I actually want to launch to the app store - it's my first so any help would be great!

    Edit/ Update 1:

    I found a small work around with this code which I put inside the second screen called DetailVC:

    // Function I use to delay hiding of views
    func delay(delay: Double, closure: ()->()) {
        dispatch_after(
            dispatch_time(
                DISPATCH_TIME_NOW,
                Int64(delay * Double(NSEC_PER_SEC))
            ),
            dispatch_get_main_queue(), closure)
    }
    
    // Hide the 2 stack views after 0.0001 seconds of screen loading
    override func awakeFromNib() {
        delay(0.001) { () -> () in
            self.summaryStackView.hidden = true
            self.combinedStackView.hidden = true
        }
    }
    
    // Update view screen elements after 0.1 seconds in viewWillAppear
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        delay(0.1) { () -> () in
            self.nameLabel.text = "John"
        }
    }
    

    This gets rid of the warnings about layout constraints completely from Xcode.

    It's still not perfect because sometimes I see a glimpse of the views that are supposed to be hidden -- they flash really quick on the screen then disappear. This happens so quickly though.

    Any suggestions as to why this gets rid of warnings? Also, any suggestions on how to improve this to work perfectly??? Thanks!