swift UIButton

    /* swift3.0  UIButton用法 */

    

    /* 1. 创建button的几种类型 

         (1)UIButtonType.system: 前面不带图标,默认文字颜色为蓝色,触摸有高亮效果

         (2)UIButtonType.custom: 定制按钮,前面不带图片,默认文字颜色为白色,无触摸时的高亮效果

         (3)UIButtonType.contactAdd: 前面带+图标按钮,默认颜色为蓝色,触摸有高亮效果

         (4)UIButtonType.....

    */

        

        

        

        /* 创建button */

        let button:UIButton = UIButton.init(type: .custom)

        button.bounds = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 100, height: 40))

        button.center = view.center

        button.setTitle("hello world", for: .normal)

        button.setTitleColor(UIColor.red, for: .normal)

        button.layer.borderColor = UIColor.red.cgColor

        button.layer.borderWidth = 1;

        view.addSubview(button)

        

        

        

    /* 2. button的四种状态 

         (1) .normal 普通状态

         (2) .highlighted  触摸状态,按下~弹起之间

         (3) .selected 选中状态

         (4) .disabled 禁用状态下的文字

         

        button上的文字,颜色,图片等需要针对不同的状态设置;

    */

       

        /* 按钮的文字设置 */

        button.setTitle("普通状态", for: .normal)

        button.setTitle("高亮状态", for: .highlighted)

        button.setTitle("禁用状态", for: .disabled)

        

        

        

        

        /* 按钮的文字颜色设置  .custom形式创建的button,文本默认为白色 */

        button.setTitleColor(UIColor.black, for: .normal)

        button.setTitleColor(UIColor.red, for: .highlighted)

        button.setTitleColor(UIColor.green, for: .selected)

        button.setTitleColor(UIColor.gray, for: .disabled)

        

        

        

        

        /* 按钮文字阴影颜色的设置 */

        button.setTitleShadowColor(UIColor.red, for: .normal)

        /* 通过button设置shadowColor之后设置 labelshadowcolor无效 */

        button.titleLabel?.shadowColor = UIColor.gray

        /* 需要设置label中的shadowOffset才来出现阴影*/

        button.titleLabel?.shadowOffset = CGSize.init(width: 1.5, height: 1.5)

        

        

        

        

        /* 不同状态下 背景图片设置 */

        button.setBackgroundImage( UIImage.init(named: "a.png"), for: .normal)

        button.setBackgroundImage( UIImage.init(named: "b.png"), for: .highlighted)

        

        /* 背景颜色 设置(不区分状态) */

        button.backgroundColor = UIColor.green;

        

        

        

        

        /* 在有些控件中设置image 会被渲染成统一颜色,而导致原来的颜色改变 */

        // 设置RenderingMode(.alwaysOriginal) 保证使用原图片

        let image = UIImage.init(named: "a.png")?.withRenderingMode(.alwaysOriginal)

        button.setImage(image, for: .selected)

        

        /* 设置取消高亮状态 */

        button.adjustsImageWhenHighlighted = false;

        /* 设置取消状态 */

        button.adjustsImageWhenDisabled = false;

        

    /* 按钮事件

            

         touchDown 单点触摸按下 事件

         touchDownRepeat 多点触摸按下事件

         touchDragInside 触摸在控件内拖动时

         touchDragOutside 触摸在控件外拖动

         touchDragEnter   触摸从控件之外拖动到内部

         touchDragExit    触摸从控件内部拖动到外部

         touchUpInside    在控件之内触摸并抬起事件

         touchUpOutside   在控件之外触摸抬起事件

         touchCancel      触摸取消事件,即一次触摸因为放上太多手指而被取消,或者被电话打断

         valueChanged     值改变

         

         用的比较多的就是touchUpInside valueChanged

         

    */

    /* 按钮事件响应 */

        button.addTarget(self, action: #selector(call), for:.touchUpInside)


        

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值