以编程方式在底部按钮

时间:2017-08-02 10:05:38

标签: ios swift uibutton

我正在尝试使用此库:

https://github.com/yannickl/DynamicButton#requirements

我需要以编程方式(Swift)将底部放在ViewController的底部,底部和按钮之间有一点距离。我怎么能这样做?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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)
相关问题