let btn = UIButton(type: .custom)
btn.frame = CGRect(x: 100, y: 200, width: 100, height: 30)
//设置按钮标题
btn.setTitle("按钮", for: .normal)
//设置标题颜色
btn.setTitleColor(UIColor.blue, for: .normal)
btn.setTitleColor(UIColor.gray, for: .highlighted)
//设置阴影颜色
btn.setTitleShadowColor(UIColor.orange, for: .normal)
//设置按钮文字大小
btn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
btn.setImage(UIImage(named:"2015122209433537916"), for: .normal)//设置图标
btn.adjustsImageWhenHighlighted=false //使触摸模式下按钮也不会变暗(半透明)
btn.adjustsImageWhenDisabled=false //使禁用模式下按钮也不会变暗(半透明)
//传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号
btn.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
//btn.addTarget(self, action:#selector(tapped(_:)), for:.touchUpInside)
//添加按钮
self.view.addSubview(btn)
//按钮文字图片
//图片不显示是因为图片尺寸太大
let btn1:UIButton = UIButton(frame: CGRect(x: 50, y: 50, width: 180, height: 32))
btn1.setImage(UIImage(named: "2015122209433537916"), for: UIControlState.normal) //按钮图标
btn1.titleLabel?.font = UIFont.boldSystemFont(ofSize: 28) //文字大小
btn1.setTitle("带图标按钮", for: UIControlState.normal) //按钮文字
btn1.setTitleColor(UIColor.orange, for: UIControlState.normal) //文字颜色
//不传递触摸对象(即点击的按钮)
btn1.addTarget(self, action: #selector(click), for: .touchUpInside)
self.view.addSubview(btn1)
func click()
{
print("点击按钮啦")
}
func btnClick(_ button:UIButton)
{
print("点击按钮啦")
}