美文网首页
4.2、UIButton

4.2、UIButton

作者: 艾希_可可 | 来源:发表于2017-06-28 10:06 被阅读21次

    classViewController:UIViewController{

        overridefuncviewDidLoad() {

            super.viewDidLoad()

    //        UIButton在我们的开发过程中是随处可见的,那么本人就其基本属性和用法进行了总结,具体如下:

    //        UIButton继承自UIControl,所以UIControl的一些属性和方法对于UIButton来说适用。

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

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

            //UIButtonType.contactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果

            //UIButtonType.detailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果

            //UIButtonType.infoDark:为感叹号“!”圆形按钮

            //UIButtonType.infoLight:为感叹号“!”圆形按钮

            //1.创建一个按钮 类型为一个枚举需要使用'.'来定义类型

            letbtn =UIButton(type: .custom)

            btn.frame=CGRect(x:100, y:105, width:150, height:40)

            //添加到view上

            self.view.addSubview(btn)

            //如果为custom类型的可以直接简化如下 默认为custom类型的

            //        let btn = UIButton(frame: CGRect(x: 5, y: 5, width: 50, height: 50))

            //3.文字设置

            btn.setTitle("嘿嘿", for: .normal)

            //4.文字颜色设置

            btn.setTitleColor(UIColor.blue, for: .normal)

            btn.setTitle("选中状态", for:UIControlState.selected) //选中状态下的文字

            btn.setTitle("触摸高亮状态", for:UIControlState.highlighted) //触摸状态下的文字

            btn.setTitle("禁用状态", for:UIControlState.disabled) //禁用状态下的文字

            //5.背景图片设置以及图片设置

            btn.setImage(UIImage.init(named:"tabbar01_selected"), for: .normal)

            letbtn2 =UIButton(type: .custom)

            btn2.frame=CGRect(x:100, y:205, width:150, height:40)

            //添加到view上

            self.view.addSubview(btn2)

            btn2.setBackgroundImage(UIImage.init(named:"navigationbar"), for: .normal)

            btn2.setTitle("设置背景色", for: .normal)

            //6.按钮背景颜色

            btn.backgroundColor = UIColor.brown

            //7.文字的位置,图片的位置

            btn.imageEdgeInsets=UIEdgeInsets(top:5, left:-15, bottom:5, right:1)

    //        btn.titleEdgeInsets =UIEdgeInsetsMake(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat)

            //8、添加事件

            btn.addTarget(self, action:#selector(touchAction), for: .touchUpInside)

            //#selector(buttonClick)    不带参数

            //#selector(buttonAtion(_:))    带参数,参数即它对象本身

            //传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上(_:)

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

            //设置选择状态(按钮是否选中)

            //9、button选中

            btn.isSelected=false  //未选中

            btn.isSelected=true  //选中

            //10、使能状态(按钮是否可用),默认是 YES 即可用

            //button不可用

            btn.isEnabled=false  //可用

            btn.isEnabled=true  //不可用

    //        11、设置圆角

            btn.layer.cornerRadius = 15

            btn.layer.masksToBounds = true

        }

        @objcfunctouchAction() {

            print("被电击了")

        }

        //btn.addTarget(self, action: #selector(changeValue(button: )), for: .touchUpInside)

        //事件的响应实现方

        funcbuttonAtions(button:UIButton){

            //对应改变按钮状态(实现选中与未选中)

            ifbutton.isSelected==false{

                button.isSelected=true;

            }else{

                button.isSelected=false;

            }

            //还有另一种方法

            //button.selected = !(button.selected);  //一句话搞定(与上面 if 判断是一样的)

        }

        @objcfuncchangeValue(button:UIButton) {

            button.setTitle(button.isSelected?"hello":"swift", for: .normal)

            button.isSelected= !(button.isSelected)

            print("change")

        }

        overridefuncdidReceiveMemoryWarning() {

            super.didReceiveMemoryWarning()

            // Dispose of any resources that can be recreated.

        }

    }

    相关文章

      网友评论

          本文标题:4.2、UIButton

          本文链接:https://www.haomeiwen.com/subject/lwaycxtx.html