它有点晚了,但也许可以帮助别人.您可以通过编程方式轻松完成.只需创建按钮的实例,设置按钮的所需属性最好在框架上使用约束,因为它会在每个屏幕尺寸的相同位置渲染按钮.我发布的代码将使按钮轮.使用约束值来改变按钮的位置和大小.
原文链接:https://www.f2er.com/swift/319007.html斯威夫特3
var roundButton = UIButton() override func viewDidLoad() { super.viewDidLoad() self.roundButton = UIButton(type: .custom) self.roundButton.setTitleColor(UIColor.orange,for: .normal) self.roundButton.addTarget(self,action: #selector(ButtonClick(_:)),for: UIControlEvents.touchUpInside) self.view.addSubview(roundButton) } override func viewWillLayoutSubviews() { roundButton.layer.cornerRadius = roundButton.layer.frame.size.width/2 roundButton.backgroundColor = UIColor.lightGray roundButton.clipsToBounds = true roundButton.setImage(UIImage(named:"your-image"),for: .normal) roundButton.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ roundButton.trailingAnchor.constraint(equalTo: self.view.trailingAnchor,constant: -3),roundButton.bottomAnchor.constraint (equalTo: self.view.bottomAnchor,constant: -53),roundButton.widthAnchor.constraint(equalToConstant: 50),roundButton.heightAnchor.constraint(equalToConstant: 50)]) } /** Action Handler for button **/ @IBAction func ButtonClick(_ sender: UIButton){ /** Do whatever you wanna do on button click**/ }