Search code examples
iosxcode5autolayout

Using Auto layout with hidden views


I have a view controller with a UIScrollView. Inside that scrollview I have two UIViews. viewA sits onto of viewB. I am using Xcode 5 and auto layout in this project. What I would like to do is when viewA is hidden, move viewB to the top where viewA was sitting (technically, still sitting, just hidden).

I looked at this SO answer: How to use auto-layout to move other views when a view is hidden?

However it hasn't really helped me. I connected a IBOutlet which was a constant to the vertical spacing to the top of the scroll view and set it to 0.0f when viewA was hidden. However it makes no changes to my UI at all.


Solution

  • First get the Top Space to SuperView NSlayoutConstraints Outlets for both subViews and then do the following:-

    [self.aView setHidden:YES];
    if([self.aView isHidden])
    {
        self.bViewTopConstraint.constant = self.aViewTopConstraint.constant;
    }
    

    using this the second UiView will go to the place of first UIView.

    For Scrollview you have to set the constraints value properly. No need to set any contentsize. Once you set the constriants scrollview will work automatically. Check the attached screenshot.

    enter image description here