您实际上可以将约束链接到完全不同的视图控制器吗? [英] Can you actually link constraints to utterly different view controllers?

查看:64
本文介绍了您实际上可以将约束链接到完全不同的视图控制器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,您有一个屏幕,其中包含许多视图控制器(带有容器视图,或其他).

Imagine you have a screen which has a number of view controllers (with container views, or whatever).

(如果您愿意,请考虑一个iPad应用程序,其中包含许多大而复杂的区域,每个区域都有用于列表,控件等的各自的子容器视图)

(If you like, think of an iPad app with many large, complicated areas, each with their own sub-container views for lists, controls, etc.)

甚至-可能是您的VC隐藏,滑出了屏幕,甚至可能只是在缓存中而未显示,或者您可能有许多屏幕彼此堆叠"在一起,所以有些甚至没有可见.

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.

所以说你有个视角v

      var v: UIView

在其中一个视图控制器中,

和一个视图w

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.

事实上.你可以NSLayoutConstraint彼此吗?

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.)

有可能吗?

其中一个VC消失时是否会引起问题(例如)?

Does it cause problems (say) when one of the VCs disappears?

(实际上约束会导致硬引用吗?)

(In fact would the constraint cause a hard reference?)

这是怎么回事?

推荐答案

是的,只要您的视图都属于同一视图层次结构,它就会起作用.它们是childViewControllers没关系,一切工作方式与普通子视图相同.

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.

这篇关于您实际上可以将约束链接到完全不同的视图控制器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆