2017-12-27 83 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 –