let btn = UIButton(type: .custom)
btn.frame = CGRect(x: 100, y: 200, width: 100, height: 30)
//設(shè)置按鈕標(biāo)題
btn.setTitle("按鈕", for: .normal)
//設(shè)置標(biāo)題顏色
btn.setTitleColor(UIColor.blue, for: .normal)
btn.setTitleColor(UIColor.gray, for: .highlighted)
//設(shè)置陰影顏色
btn.setTitleShadowColor(UIColor.orange, for: .normal)
//設(shè)置按鈕文字大小
btn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
btn.setImage(UIImage(named:"2015122209433537916"), for: .normal)//設(shè)置圖標(biāo)
btn.adjustsImageWhenHighlighted=false //使觸摸模式下按鈕也不會變暗(半透明)
btn.adjustsImageWhenDisabled=false //使禁用模式下按鈕也不會變暗(半透明)
//傳遞觸摸對象(即點擊的按鈕),需要在定義action參數(shù)時,方法名稱后面帶上冒號
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) //按鈕圖標(biāo)
btn1.titleLabel?.font = UIFont.boldSystemFont(ofSize: 28) //文字大小
btn1.setTitle("帶圖標(biāo)按鈕", 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("點擊按鈕啦")
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。