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

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.

Related

How can i resize the parent view after changing the constraints of child views?

I have a pop up view which is loading from a xib file.In that file there are couple of views, labels and buttons. For specific condition i have hide couple of views and buttons and its working but total height of the parent view of those views and buttons is not changing.
I have set the height constraints of the parent view and tried to change the constant but its not updating.
self.translatesAutoresizingMaskIntoConstraints = YES;
I want to resize the parent view after hide the parent view.
Main window
Changed window after constraints
Hiding the view does not remove it from its parents. It just changes its visibility. The view still remains in the hierarchy. To achieve what you want to do the following:
Constraint the view above and view below the view you want to hide with a lower priority.
remove the view(the one you want to hide) from its superview
I hope this helps

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?

Hide non child UIView in UIView

I want to hide a non child UIView in a UIView but I don't know how to do it.
To be specific, I have a UITableView. Each UITableViewCell has another view inside it (a wrapper view called wrapperView). The wrapper has some labels set up in IB and some created programmatically. I have created a custom slide mechanism that reveals buttons under the wrapper (like the standard one does).
The labels created programmatically don't exceed wrapper's bounds because it clips the subviews. The problem is with the labels created in IB. They are the subviews of contentView.
How can this be solved? Is there a way for a UIView to clip other views on the same level(not parents nor children)? Or "transfer" the labels to the wrapper view?
It isn't completely clear what you're asking. A view will only clip it's subviews, not views that happen to fall within their frame rectangle but aren't subviews.
If you want to move a view object from one view hierarchy to another you can use addSubview(_:) to do so. That will automatically remove it from it's current parent view. To quote the Apple docs:
Views can have only one superview. If view already has a superview and
that view is not the receiver, this method removes the previous
superview before making the receiver its new superview.

Rearrange UIView Subviews if a Subview is removed in Auto Layout

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.

When a UIView does not have a superview when loaded in iOS app?

From developer reference:
(void)sizeToFit
Call this method when you want to resize the current view so that it
uses the most appropriate amount of space. Specific UIKit views resize
themselves according to their own internal needs. In some cases, if a
view does not have a superview, it may size itself to the screen
bounds. Thus, if you want a given view to size itself to its parent
view, you should add it to the parent view before calling this method.
Is there a case when a view does not have a superview? What are those cases?
a view doesnt have a superview when the view is the rootview meaing that there's no other view behind it (besides the window possibly)
OR when it was never added to a view obviously :D
If it is your View Controller's view, it doesn't have a superview.

Resources