button swift

//
// ViewController.swift
// UILabelTest
//
// Created by mac on 15/6/23.
// Copyright (c) 2015年 fangyuhao. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    var button:UIButton = UIButton.buttonWithType(UIButtonType.ContactAdd) as UIButton
    button.frame = CGRectMake(10, 150, 100, 30)

// var button:UIButton = UIButton(frame: CGRectMake(10, 150, 100, 30))
//设置按钮文字、颜色和状态
button.setTitle("普通状态", forState: UIControlState.Normal)
button.setTitle("触摸状态", forState: UIControlState.Highlighted)
button.setTitle("禁用状态", forState: .Disabled)

    button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    button.setTitleColor(UIColor.greenColor(), forState: UIControlState.Highlighted)
    button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Disabled)

    button.setTitleShadowColor(UIColor.greenColor(), forState: UIControlState.Normal)
    button.setTitleShadowColor(UIColor.yellowColor(), forState: UIControlState.Highlighted)
    button.setTitleShadowColor(UIColor.grayColor(), forState: UIControlState.Disabled)
    //设置按钮的背景颜色

// button.backgroundColor = UIColor.blackColor()

    //按钮的图片
    button.setImage(UIImage(named: "icon"), forState: UIControlState.Normal)
    button.adjustsImageWhenDisabled = false//禁用状态时,图标不变暗
    button.setBackgroundImage(UIImage(named: "green"), forState: UIControlState.Normal)

    //按钮的触摸事件
    button.addTarget(self, action: Selector("tapped"), forControlEvents: UIControlEvents.TouchUpInside)
    //若要tapped方法带参数

// button.addTarget(self, action: Selector("tapped:"), forControlEvents: UIControlEvents.TouchUpInside)

    self.view.addSubview(button)



    // 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.
}
func tapped(){
    println("tapped")

}
func tapped(button:UIButton){
    println("tapped")

// println(button.titleForState(.Normal))

}

}

posted @ 2016-12-16 23:55  乱笙歌  阅读(143)  评论(0编辑  收藏  举报