2017-06-29 63 views
1
背景一個UIView

我純粹代碼創建一個控制器上的UIView(我不是在所有用故事板創建斯威夫特

這裏爲簡單的視圖代碼:

let greenView: UIView = { 
    let v = UIView(frame: CGRect.zero) 
    v.translatesAutoresizingMaskIntoConstraints = false 
    v.backgroundColor = UIColor.rgb(red: 183, green: 202, blue: 45) 
    v.layer.borderWidth = 1 
    return v 
}() 

當我設置在屏幕的觀點,我有:

view.addSubview(greenView) 
    greenView.anchor(top: joined.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 20, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: view.frame.width - 20, height: 200) 

這個「錨」的功能僅僅是一個擴展,以幫助建立汽車佈局約束和它運作良好,包括像所有其他組件實際上從UIView繼承的UIButton。

問題:視圖不顯示在屏幕上的任何位置......我錯過了什麼嗎?我正在使用LBTAComponents

更具體地說this class 到目前爲止工作良好與所有其他方法。

+0

該行爲表明約束不起作用。爲什麼不顯示你正在使用的擴展名?更好的是,不要使用擴展名 - 只需將「long」代碼添加到此視圖中,並查看它是否有效。如果是這樣,那麼就「向後」工作到擴展。 – dfd

+0

您是否在[查看調試器]中查找了視圖(https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/debugging_with_xcode/chapters/special_debugging_workflows.html#//apple_ref/doc/uid/ TP40015022-CH9-SW2)?確保在調試器中關閉剪輯,以便即使裁剪也可以看到該視圖。 –

+0

@robmayoff它顯示在堆棧上,但它表示高度不明確。 –

回答

0

你說你正在使用LBTAComponents,但你的方法簽名不匹配。它們很接近,但看起來他們使用的是舊版本(查看歷史記錄,我沒有看到與上面使用的方法簽名匹配的舊版本),或者您有自己的擴展包裝此API 。例如,下面的代碼:

greenView.anchor(top: joined.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 20, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: view.frame.width - 20, height: 200) 

應該是:

greenView.anchor(joined.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, topConstant: 20, leftConstant: 20, bottomConstant: 0, rightConstant: 0, widthConstant: view.bounds.width - 20, heightConstant: 200) 

當我糾正方法簽名,並與LBTAComponents的最新版本中使用它,代碼工作正常。所以這個問題必須在這些擴展的版本中(或者你用來包裝這個庫以產生上面的API的任何層)。

+0

感謝這是真的,我從這個LBTAComponents的作者在YouTube上跟着一些教程,我有一箇舊的擴展與該錨方法...愚蠢的我..非常感謝@Rob。 –