2017-12-27 260 views
0

这里是我的应用程序,左侧的配置文件(ProfileViewController)和右侧的容器视图(ContainerViewController)。我试图实现的是一旦containerViewController完成了它的工作,它将更新UIView的高度。修改容器视图的高度

enter image description here

// ContainerViewController.swift 
let heightConstraint = self.view.constraints.filter { $0.identifier == "Height" }.first 
heightConstraint?.constant = height 

当浏览限制的名单,他们都是emtpy,我也设立在故事板的一些限制。所以为什么 ?有什么方法可以访问ContainerViewController中的UIView?

PS:

DispatchQueue.main.async { 
    self.view.frame = CGRect(x: 0, y: 0, width: width, height: height) 
} 

听起来工作,但我认为,修改约束是正确的?不 ?

回答

1

我想要做的是为约束设置IBOutlet,并将其存储在ContainerView中。

即使未来代码(或故事板)发生更改,这种方式也不会依赖订单号或其他任何方式来获取此约束。

是的,正确的方法是设置约束常数,而不是改变框架。 但请注意,即使是约束条件的不断变化也需要放在主(UI)线程上 - (根据您的代码,我假设您正在处理线程)。

+0

我无法为containerViewController中的约束设置IBOutlet,它只能在ProfileViewController中链接它时才起作用。这意味着使用协议和containerViewController将更新baseController(ProfileViewController)。是否有任何其他方式让所有的控制器独立,并确实将IBoulet链接到containerViewController? – Alex

+0

如果你在containerViewController中有约束,它应该可以工作,但是如果你在ProfileViewController中约束并且只是“调节”containerViewController。 (看故事板树)。在初始化视图后,您可以将ProfileViewController的约束传递给containerViewController –