Ios Swift 3:将UIButton扩展添加到ViewController
我是iOS/Swift的初学者,尝试创建一个没有故事板的简单应用程序。 我创建了一个Ios Swift 3:将UIButton扩展添加到ViewController,ios,swift3,uibutton,swift-extensions,Ios,Swift3,Uibutton,Swift Extensions,我是iOS/Swift的初学者,尝试创建一个没有故事板的简单应用程序。 我创建了一个ui按钮扩展,我想在我的视图中添加一个简单的按钮(稍后将设置约束)。 不幸的是,按钮不可见。 如果有人帮助我,我将不胜感激。 谢谢大家! ---斯威夫特--- ---InitialViewController.swift--- 试试这个: extension UIButton { func createRectangleButton(buttonPositionX: Double, buttonPositi
ui按钮
扩展,我想在我的视图中添加一个简单的按钮(稍后将设置约束)。
不幸的是,按钮不可见。
如果有人帮助我,我将不胜感激。
谢谢大家!
---斯威夫特---
---InitialViewController.swift---
试试这个:
extension UIButton {
func createRectangleButton(buttonPositionX: Double, buttonPositionY: Double ,buttonWidth: Double, buttonHeight: Double, buttonTilte: String) {
let button = self // changes made here
button.frame = CGRect(x: buttonPositionX, y: buttonPositionY, width: buttonWidth, height: buttonHeight)
button.setTitle(buttonTilte, for: .normal)
button.backgroundColor = COLOR_WHITE
button.tintColor = COLOR_BLACK
}
}
我宁愿建议创建一个自定义初始值设定项。
import UIKit
class InitialViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Gradient Layer
view.addGradientBackground(colorTop: COLOR_ROYALRED2, colorBottom: COLOR_ROYALRED1)
// Button
let startButton = UIButton()
startButton.createRectangleButton(buttonPositionX: 50, buttonPositionY: 20, buttonWidth: 200, buttonHeight: 50, buttonTilte: "START")
self.view.addSubview(startButton)
}
}
extension UIButton {
func createRectangleButton(buttonPositionX: Double, buttonPositionY: Double ,buttonWidth: Double, buttonHeight: Double, buttonTilte: String) {
let button = self // changes made here
button.frame = CGRect(x: buttonPositionX, y: buttonPositionY, width: buttonWidth, height: buttonHeight)
button.setTitle(buttonTilte, for: .normal)
button.backgroundColor = COLOR_WHITE
button.tintColor = COLOR_BLACK
}
}