问题
Imagine you have a screen which has a number of view controllers (with container views, or whatever).
(If you like, think of an iPad app with many large, complicated areas, each with their own sub-container views for lists, controls, etc.)
Even - it may be that you have VCs which are hidden, slidden off screen, perhaps even just in cache and not presented, or you may have a number of screens "stacked on top" of one another so some are not even visible.
So say you have a view v
var v: UIView
in one of those view controllers, and a view w
var w: UIView
in a totally different view controller.
For some reason you want the two items to have the same width, on screen.
In fact. Can you NSLayoutConstraint one to the other??
(Obviously you can't in storyboard; that's no big deal. You'd do it in code at layout time.)
Is that possible?
Does it cause problems (say) when one of the VCs disappears?
(In fact would the constraint cause a hard reference?)
What's the deal on this?
回答1:
Yes it will work, as long as your views are all part of the same view hierarchy. It doesn't matter that they are childViewControllers, everything works the same way as it would with normal subviews.
You can only add constraints to views that are already added to the same view, and you'll get an exception if you try to add constraints to views that haven't yet been added.
If you remove any of the views, their associated constraints will also be removed.
来源:https://stackoverflow.com/questions/47239151/can-you-actually-link-constraints-to-utterly-different-view-controllers