2017-08-02 65 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