我有一個自定義的視圖,我已經實現了,並且正在嘗試學習如何在不使用故事板的情況下使用自動佈局。我的問題是我需要使用框架來初始化視圖,還是隻需要執行一個空的init,然後使用自動佈局讓視圖自行計算寬度和高度?下面你會看到我已經嘗試添加框架,然後把自動佈局思考,我必須先放置一個框架,然後自動佈局改變它。視圖是否需要使用框架初始化,然後自動佈局更改或僅使用自動佈局?
let origin = CGPoint(x: 0.0, y: 0.0)
let size = CGSize(width: view.frame.width, height: 44.0)
let frame = CGRect(origin: origin, size: size)
customView = ExpandableHeadingView(frame: headingFrame)
customView.frame.size.height = 44.0
view.addSubview(customView)
view.topAnchor.constraint(equalTo: customView.topAnchor).isActive = true
view.leftAnchor.constraint(equalTo: customView.leftAnchor).isActive = true
view.rightAnchor.constraint(equalTo: customView.rightAnchor).isActive = true
view.heightAnchor.constraint(equalTo: customView.heightAnchor).isActive = true
我做你的建議,但它是還是行不通。我已經添加了這個'customView.heightAnchor.constraint(equalToConstant:44.0)'希望這會讓它顯示出來,但沒有任何東西。我甚至調試了視圖,約束沒有顯示出來。有關爲什麼高度限制不起作用的任何想法?我有一個左,右,和頂部設置,所以高度應該照顧我心中的最後一點。 – user3832583
當你說它不工作時,你是什麼意思?究竟發生了什麼?我也看到你已經有一個高度約束設置在:view.heightAnchor.constraint(equalTo:customView.heightAnchor).isActive = true'。你刪除了這個嗎? – Dima