设置按钮类型

Pet*_*zen 3 uibutton ios swift xcode6

我正在以编程方式创建一个按钮,我想知道如何设置该按钮的UIButtonType.

这就是我创建按钮的方式:

let headerPlusButon:UIButton = UIButton(frame: CGRectMake(5, tableView.frame.width - 30, 20, 20))
headerPlusButon.addTarget(self, action: "paymentAddButtonTapped", forControlEvents:.TouchUpInside)
Run Code Online (Sandbox Code Playgroud)

没有"setButtonTypeFor ..."方法,比如你如何更改标题的文本,显然这不起作用:

headerPlusButon.buttonType = UIButtonType.ContactAdd
Run Code Online (Sandbox Code Playgroud)

Abh*_*rma 11

override func viewDidLoad() {

    super.viewDidLoad()

    let button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
    button.frame = CGRectMake(100, 100, 100, 50)
    button.backgroundColor = UIColor.greenColor()
    button.setTitle("Test Button", forState: UIControlState.Normal)
    button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

    self.view.addSubview(button)
}  

func buttonAction(sender:UIButton!)
{
    println("Button tapped")
}
Run Code Online (Sandbox Code Playgroud)

从下面的swift 2替换UIButton声明.

let button = UIButton(type: .System) as UIButton
Run Code Online (Sandbox Code Playgroud)