Swift_UIButton

来源:互联网 发布:能做淘宝浏览单的app 编辑:程序博客网 时间:2024/06/01 07:34

UIButton的创建方式:

1.
let button = UIButton(frame: CGRectMake(10, 30, 60, 50))
2.
var button:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton                button.frame = CGRectMake(10, 100, 100, 50)

基本使用

   func setButton() {        let button = UIButton(frame: CGRectMake(10, 30, 100, 40))        //设置背景颜色        button.backgroundColor = UIColor.brownColor()        //设置标题        button.setTitle("点击", forState: UIControlState.Normal)        //设置标题颜色        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)        //设置图片        button.setImage(UIImage(named: "buttonadd"), forState:UIControlState.Normal)        //设置图片偏移量        button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 20)        //添加触发事件 如果事件需要传递参数 即在方法名后添加':' eg:"swiftBtnClick:"        button.addTarget(self, action:Selector("swiftBtnClick:"), forControlEvents:UIControlEvents.TouchUpInside)        //添加到视图上        self.view.addSubview(button)    }        func swiftBtnClick(button:UIButton) {        println("被点击")    }

这里需要注意的是 如果按钮的触发方法带参数 需要在Selector里面填写方法名称后面添加冒号
<span style="color:#ffcccc;"> </span> func setHeighLightButton() {        var hlBtn = UIButton.buttonWithType(UIButtonType.System) as! UIButton                hlBtn.frame = CGRectMake(10, 160, 100, 40)        hlBtn.setTitle("普通", forState: UIControlState.Normal)//        hlBtn.setTitle("高亮", forState: UIControlState.Highlighted)        hlBtn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)                hlBtn.setTitle("以选中", forState: UIControlState.Selected)        hlBtn.setTitleColor(UIColor.redColor(), forState: UIControlState.Selected)                hlBtn.setBackgroundImage(UIImage(named: "backbutton_green"), forState: UIControlState.Normal)        hlBtn.setBackgroundImage(UIImage(named: "backbutton_yellow"), forState: UIControlState.Highlighted)        hlBtn.addTarget(self, action: Selector("hlBtnClick"), forControlEvents: UIControlEvents.TouchUpInside)                //引用        _lightBtn = hlBtn                self.view.addSubview(hlBtn)    }        func hlBtnClick() {        _lightBtn!.selected = !_lightBtn!.selected                //.currentTitle 只能打印Normal状态下的按钮标题        println(_lightBtn!.titleForState(UIControlState.Selected))            }

常用API

    func setTitle(title: String?, forState state: UIControlState) // 设置那种状态下的标题    func setTitleColor(color: UIColor?, forState state: UIControlState) // 设置哪种状态下的标题颜色    func setTitleShadowColor(color: UIColor?, forState state: UIControlState) //设置哪种状态下的标题阴影颜色    func setImage(image: UIImage?, forState state: UIControlState) // 设置图片 需要保证不同状态下图片尺寸相同    func setBackgroundImage(image: UIImage?, forState state: UIControlState) // 设置背景图片    @availability(iOS, introduced=6.0)    func setAttributedTitle(title: NSAttributedString!, forState state: UIControlState) //利用属性字符串 保证标题为单行            //获取当前状态下的按钮一些属性    func titleForState(state: UIControlState) -> String? // these getters only take a single state value    func titleColorForState(state: UIControlState) -> UIColor?    func titleShadowColorForState(state: UIControlState) -> UIColor?    func imageForState(state: UIControlState) -> UIImage?    func backgroundImageForState(state: UIControlState) -> UIImage?    @availability(iOS, introduced=6.0)    func attributedTitleForState(state: UIControlState) -> NSAttributedString?


0 0
原创粉丝点击