5、UIButton并添加png图片资源在代码中使用

1、代码如下:


import UIKit

class ViewController: UIViewController {
    var btn:UIButton!
    var btn1:UIButton!
    var btn2:UIButton!
    var btn3:UIButton!
    var btn4:UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        btn=UIButton(type:UIButtonType.contactAdd)//前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
        btn.frame=CGRect(x:10,y:50,width:100,height:30)
        btn.setTitle("普通状态", for: UIControlState.normal)//普通状态下的文字
        btn.setTitle("触摸状态", for: UIControlState.highlighted)//触摸状态下的文字
        btn.setTitle("禁用状态", for: UIControlState.disabled)//禁用状态下的文字
        btn.setTitleColor(UIColor.brown, for: UIControlState.normal)//普通状态下的颜色
        btn.setTitleColor(UIColor.cyan, for: UIControlState.highlighted)//触摸状态下的颜色
        btn.setTitleColor(UIColor.gray, for: UIControlState.disabled)//禁用状态下的颜色
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 10)//按钮文字的字体和大小设置
        btn.setTitleShadowColor(UIColor.green, for:UIControlState.normal) //普通状态下文字阴影的颜色
        btn.setTitleShadowColor(UIColor.yellow, for:UIControlState.highlighted) //普通状态下文字阴影的颜色
        btn.setTitleShadowColor(UIColor.gray, for:UIControlState.disabled) //普通状态下文字阴影的颜色
        btn.backgroundColor = UIColor.black//按钮背景颜色设置
        btn.setImage(UIImage(named:"ico"),for:UIControlState.normal)  //设置图标
        btn.adjustsImageWhenHighlighted=false //使触摸模式下按钮也不会变暗(半透明)
        btn.adjustsImageWhenDisabled=false //使禁用模式下按钮也不会变暗(半透明)
        self.view.addSubview(btn)
        
        btn1=UIButton(type:UIButtonType.system)//前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
        btn1.frame=CGRect(x:130,y:50,width:100,height:30)
        btn1.setTitle("普通状态", for: UIControlState.normal)//普通状态下的文字
        btn1.setTitle("触摸状态", for: UIControlState.highlighted)//触摸状态下的文字
        btn1.setTitle("禁用状态", for: UIControlState.disabled)//禁用状态下的文字
        btn1.setTitleColor(UIColor.brown, for: UIControlState.normal)//普通状态下的颜色
        btn1.setTitleColor(UIColor.cyan, for: UIControlState.highlighted)//触摸状态下的颜色
        btn1.titleLabel?.font = UIFont.systemFont(ofSize: 20)//按钮文字的字体和大小设置
        btn1.setTitleColor(UIColor.gray, for: UIControlState.disabled)//禁用状态下的颜色
        btn1.setTitleShadowColor(UIColor.green, for:UIControlState.normal) //普通状态下文字阴影的颜色
        btn1.setTitleShadowColor(UIColor.yellow, for:UIControlState.highlighted) //普通状态下文字阴影的颜色
        btn1.setTitleShadowColor(UIColor.gray, for:UIControlState.disabled) //普通状态下文字阴影的颜色
        btn1.backgroundColor = UIColor.lightGray//按钮背景颜色设置
        btn1.setImage(UIImage(named:"get-info-blue-button"),for:UIControlState.normal)  //设置图标
        btn1.adjustsImageWhenHighlighted=false //使触摸模式下按钮也不会变暗(半透明)
        btn1.adjustsImageWhenDisabled=false //使禁用模式下按钮也不会变暗(半透明)
        
        btn1.setBackgroundImage(UIImage(named:"2-0btn_34"), for:UIControlState.normal)
        
        btn1.addTarget(self, action: #selector(clickBtn(_:)), for: UIControlEvents.touchDown)
        /*事件方式
        touchDown:单点触摸按下事件,点触屏幕
        touchDownRepeat:多点触摸按下事件,点触计数大于1,按下第2、3或第4根手指的时候
        touchDragInside:触摸在控件内拖动时
        touchDragOutside:触摸在控件外拖动时
        touchDragEnter:触摸从控件之外拖动到内部时
        touchDragExit:触摸从控件内部拖动到外部时
        touchUpInside:在控件之内触摸并抬起事件
        touchUpOutside:在控件之外触摸抬起事件
        touchCancel:触摸取消事件,即一次触摸因为放上太多手指而被取消,或者电话打断
         */
        self.view.addSubview(btn1)
        
        btn2=UIButton(type:UIButtonType.custom)//定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
        btn2.frame=CGRect(x:250,y:50,width:150,height:30)
        btn2.setTitle("普通状态", for: UIControlState.normal)//普通状态下的文字
        btn2.setTitle("触摸状态", for: UIControlState.highlighted)//触摸状态下的文字
        btn2.setTitle("禁用状态", for: UIControlState.disabled)//禁用状态下的文字
        btn2.setTitleColor(UIColor.brown, for: UIControlState.normal)//普通状态下的颜色
        btn2.setTitleColor(UIColor.cyan, for: UIControlState.highlighted)//触摸状态下的颜色
        btn2.titleLabel?.font = UIFont.systemFont(ofSize: 30)//按钮文字的字体和大小设置
        btn2.setTitleColor(UIColor.gray, for: UIControlState.disabled)//禁用状态下的颜色
        btn2.setTitleShadowColor(UIColor.green, for:UIControlState.normal) //普通状态下文字阴影的颜色
        btn2.setTitleShadowColor(UIColor.yellow, for:UIControlState.highlighted) //普通状态下文字阴影的颜色
        btn2.setTitleShadowColor(UIColor.gray, for:UIControlState.disabled) //普通状态下文字阴影的颜色
        btn2.backgroundColor = UIColor.cyan//按钮背景颜色设置
        btn2.setImage(UIImage(named:"get-info-purple-button"),for:UIControlState.normal)  //设置图标
        btn2.adjustsImageWhenHighlighted=false //使触摸模式下按钮也不会变暗(半透明)
        btn2.adjustsImageWhenDisabled=false //使禁用模式下按钮也不会变暗(半透明)
        self.view.addSubview(btn2)
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @objc func clickBtn(_ sender:UIButton){
        print(btn1.title(for: UIControlState.normal))
    }

}

2、运行效果如下:


3、向工程中添加png资源,首先打开工程中的Assets.scassets文件,如下图所示:


4、找到png图片拖放到Assets.scassets文件中,如下图所示:


5、拖放之后松开鼠标后就会自动添加到Assets.scassets文件中,如下图所示的add-1:


6、在代码中使用add-1,将原代码中的

btn.setImage(UIImage(named:"ico"),for:UIControlState.normal)更改为:

btn.setImage(UIImage(named:"add-1"),for:UIControlState.normal) ,如下图运行所示:


7、此时出现一个问题,原图为:,估计是Xcode给优化了,目前是个问题







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值