2017-01-29 24 views
0

我在我的Xcode項目中安裝了LBTAComponents框架。現在我想放在頁面頂部我的按鈕,下面是我的代碼:TopAnchor 0顯示在中間

button.anchor(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)`. 

這段代碼的令人痛心的問題是,它顯示在頁面的中間,而不是在峯會上我的光榮按鈕。我如何讓我的按鈕出現在我的頁面頂部。

回答

0

想一想你做了什麼:你已經將按鈕的所有4個邊(左邊,右邊,頂部和底部)固定到視圖的4邊 - 你怎麼能期望按鈕位於頂部你想要嗎?

你應該底部參數,設置按鈕高度使用nil在最後一個參數 - heightConstant

+0

感謝它的工作 – user7444742

0

SwiftStudier's Answer應該工作,但你並不需要的,如果你不設置高度爲最後一個參數噸需要一個特定的按鈕:

let button = UIButton(type: .system) 
button.setTitle("Hello", for: .normal) 
button.backgroundColor = .red 
view.addSubview(button) 
button.anchor(view.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0) 

結果:

enter image description here