// テキストボタン
modeButton = makeButton(CGRect(x: 0,y: 0,width: 150,height: 50), title: “ABCDE”, size: 21, tag: 0)
modeButton.layer.position = CGPoint(x:95, y:75)
self.view.addSubview(modeButton)
func makeButton(_ frame: CGRect, title: String, size:CGFloat, tag: Int) -> UIButton {
let button = UIButton()
button.frame = frame
button.backgroundColor = UIColor.blue
button.tag = tag
button.layer.masksToBounds = true
button.layer.cornerRadius = 5.0
button.setTitle(title, for: UIControl.State())
button.titleLabel?.font = UIFont.systemFont(ofSize: size)
button.addTarget(self, action: #selector(self.onClick(_:)),for: UIControl.Event.touchUpInside)
return button
}
// イメージボタン
editButton = makeImageButton(CGRect(x: 0,y: 0,width: 50,height: 50), imageName: “editIcon.png”, tag: 1) editButton.layer.position = CGPoint(x:35, y:35)
self.view.addSubview(editButton)
func makeImageButton(_ frame: CGRect, imageName: String, tag: Int) -> UIButton {
let button = UIButton(type: UIButton.ButtonType.system)
button.frame = frame
button.setBackgroundImage(UIImage(named: imageName), for: UIControl.State())
button.tag = tag
button.addTarget(self, action: #selector(self.onClick(_:)),for: UIControl.Event.touchUpInside)
return button
}
@objc func onClick(_ sender: UIButton) {
// code
}