2017-08-02 66 views

回答

1

使用自動佈局

self.view.addSubview(button) 

button.translatesAutoresizingMaskIntoConstraints = false 

button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true 
button.widthAnchor.constraint(equalToConstant: 50).isActive = true 
button.heightAnchor.constraint(equalToConstant: 50).isActive = true 
button.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -20).isActive = true 
+0

您必須首先將子視圖添加到您的視圖中 – Phyber

+0

完美的伴侶,一如既往的好,樂於助人!十分感謝! –

0
let bottomOffset = 20 

let yourButton = UIButton(frame: CGRect(X:0, y:0, width:50, height:50)) 
yourButton.backgroundColor = UIColor.red 
yourButton.frame.origin = CGPoint(X:0, y:self.view.framse.size.height - yourButton.frame.size.height - bottomOffset) 

self.view.addsubview(yourButton) 
+0

我認爲使用自動佈局更容易實現,並且更易於理解+您可以輕鬆更改任何內容,而無需任何計算和額外變量。 – Phyber

+0

自動佈局更易於理解。但我個人更喜歡用代碼,因爲它對我來說更容易維護。 –

+0

是的,我更喜歡故事板上的代碼,但我使用約束和錨來實現響應式佈局。 – Phyber