Rearrange UIView Subviews if a Subview is removed in Auto Layout - ios

I am in a need of implementing a drag and drop controller in Full-Auto Layout environment.
I have achieved the dragging and dropping successfully, but the issue is if I drag a subview from another view the other related subviews behave weirdly,
as an example the subviews are laid out horizontally in scrollview are related to each other, so if I remove a subview the other views should rearrange them self automatically.
Regards,
Dhanesh.

when you doing the design part of xib uncheck the autolayout.
After completing the designing then go towards to autolayout and arrange the view using constraint adding.

Related

How to preserve NSLayoutConstraints when rearranging hierarchy

I made a custom UIView and made the UI with an XIB and the hierarchy is like below
The highlighted view has many constraints hooked with its superview. Now I want to make the view the subview of the vibrancy effect view, AND preserve the constraints between the highlighted view and the current superview.
Right now if I just drag the view inside, the constraints are removed.
Does anyone know how I can achieve this?

Is there a way to keep subviews of a UIStackView participating in autolayout when they're hidden?

I introduced some UIStackView's in my latest project, because it made the spacing of the views and adding other autolayout constraints a lot easier.
Only to discover that hidden views inside a UIStackView no longer 'participate' when autolayout does its thing.
I suppose this is often a great feature, but for this particular view I don't want that, is there a way to have the subviews of a UIStackView behave as if they were embedded in a plain UIView?
Or do I have no option but to resort to removing the UIStackViews? (and adding a whole lot of annoying 'spacer' views and constraints)
It is by design that hidden arranged subviews are not only hidden but no longer contribute to the layout either. It's a major feature that cannot be achieved easily with auto layout.
If you want to prevent this, then you can wrap your view within another view. Instead of hiding the direct subview of the UIStackView (the wrapper view in the new setup), hide the inner view (the same way as in the old setup except it is now nested). As the direct subview is visible, UIStackView won't reclaim the space. But the user can't see any content as the view content is hidden.
Instead of hiding the subview, you can just turn its alpha to 0. This way the subview won't be visible but it will participate in the layout.

Embedding a view with subviews in another parent view and keeping constraints (IB)

I have a scene in storyboard with several subviews for which I already set a lot of constraints in Interface Builder. These subviews are directly children of the view controller's view. Now I realize that I need the whole view + subviews be contained in a full-screen UIScrollView to handle scrolling of the content when the keyboard is shown. But if I simply move all the subviews inside the scroll view, I loose the constraints that referred to "superview" and I get a mess.
Is there any way to migrate the constraints regarding the previous "superview" (view controller's view) to the new parent scroll view (which is the child of the "superview")?
Thanks
No there is no way to do this. The constraints between the view controller's view and the subviews are as you know broken when you move your subviews into another subview/view. AFIAK the biggest reason for this is you could end up with subviews that have constraints which relate to views on longer in their
view hierarchy which is a violate of the constraints lay out system.

Auto-Layout UIScrollView and an animated UIView

Currently having an issue with using auto-layout and UIScrollView. The issue is when I scroll down on the scroll view, a previous UIView (that should be free roaming, and is animated to various positions) gets set back to its initial location where it is located in Storyboard.
What's the best way to make my single UIView not follow the behavior of auto-layout and let it do whatever it wants to do?

Adding a UIScrollView to an existing UIView in Storyboard

I build an app using storyboards and I want to embed one of the view in a UIScrollView. Therefore to do so, I drag an drop the UIScrollView directly in the storyboard. I then adapt it size and position.
After that I select all my items and drag and drop them into the UIScrollView. My problem is that all my items' positions get messed up: they all align in the center of the UIScrollView so I have to reposition them myself.
Is there any way I can keep the same layout?
Thanks a lot for your help!
Cheers
One way is to select the view, then go to editor -> embed in -> scroll view.
For more complex things, temporarily disable autolayout, create a new empty viewcontroller, copy/paste all your subitems there, do your voodoo with your scrollview, paste them back when it's done.

Resources