加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift控件之旅之UIButton

发布时间:2020-12-14 01:32:32 所属栏目:百科 来源:网络整理
导读:UIButton 属性设置如下: ///---创建button func CreateButton() { ///----实例化按钮对象 let button = UIButton.init(type:UIButtonType.System); ///--设置按钮的位置和大小 button.frame = CGRect(x:10,y:150,width:100,height:30); ///---设置按钮的文

UIButton

属性设置如下:

///---创建button
    func CreateButton()
    {
        ///----实例化按钮对象
        let button = UIButton.init(type:UIButtonType.System);
        ///--设置按钮的位置和大小
        button.frame = CGRect(x:10,y:150,width:100,height:30);
        ///---设置按钮的文字
        button.setTitle("点我试试",forState:.Normal);
        ///---设置按钮的字体颜色
        button.setTitleColor(UIColor.greenColor(),forState:.Highlighted);
        button.setTitleShadowColor(UIColor.grayColor(),forState: .Disabled);
        ///---设置字体阴影
        button.setTitleShadowColor(UIColor.greenColor(),forState:.Highlighted);
        button.setTitleShadowColor(UIColor.greenColor(),forState:.Disabled);
        
        ///---设置按钮的背景色
        button.backgroundColor = UIColor.whiteColor();
        
        ///---设置按钮被触摸的事件
        button.addTarget(self,action: Selector("tapped"),forControlEvents: UIControlEvents.TouchUpInside);
        
        self.view.addSubview(button);
    }
    
    func tapped()
    {
        print("触发了触摸事件");
    }


说明:

按钮的样式的参数,如下:

UIButtonType.contanctAdd : "+"图标,默认文字为蓝色,有触摸时的高亮效果

UIButtonType.DEtailDiscloure : "!" 图标,默认文字颜色为蓝色,有触摸时的高亮效果

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

UIButtonType.Custom : 定制按钮,不带图标,默认文字颜色为蓝色,有触摸时的高亮效果


按钮的事件:

如果想在tapped 函数中获得按钮对象,需要定义action 的时候,方法名称后带上冒号。如:

button.addTarget(self,action: Selector("tapped:"),forControlEvents: UIControlEvents.TouchUpInside);
然后在方法中可以获得按钮对象了:
func tapped(button : UIButton)
{
      print(button.titleState(.Normal));
}

运行结果:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读