mport UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .custom)
button.frame = CGRect(x: 150, y: 250, width: 100, height: 100)
button.setTitle("按钮", for: .highlighted)
button.setTitleColor(UIColor.black, for: .normal)
button.setTitleShadowColor(UIColor.gray, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize:11)
button.titleLabel?.font = UIFont(name: "Farah", size: 11)
button.backgroundColor = UIColor.orange
button.setImage(UIImage(named:"icon"), for: .normal)
button.adjustsImageWhenHighlighted = false
button.adjustsImageWhenDisabled = false
let image = UIImage(named: "icon")?.withRenderingMode(.alwaysOriginal)
button.setImage(image, for: .normal)
button.setBackgroundImage(UIImage(named: "Image"), for: .normal)
button.titleLabel?.lineBreakMode = .byClipping
button.addTarget(self, action: #selector(ViewController.TapButton), for: .touchUpInside)
self.view.addSubview(button)
}
@objc func TapButton()
{
print("你点击了此按钮")
}
}
Button设置动态图片
import UIKit
class ViewController: UIViewController,UITextFieldDelegate{
override func viewDidLoad() {
super.viewDidLoad()
let image = UIImage.animatedImageNamed("animation", duration: 2.0)
let button = UIButton(frame: CGRect(x: 150, y: 250, width: 100, height: 100))
button.setImage(image, for: .normal)
self.view.addSubview(button)
}
}