设置按钮类型

Set button type

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

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

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

没有 "setButtonTypeFor..." 更改标题文本的方法,显然这行不通:

headerPlusButon.buttonType = UIButtonType.ContactAdd
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")
}

swift 2UIButton 声明替换为以下行。

let button = UIButton(type: .System) as UIButton

这是我的代码

let tutorialButton = UIButton.init(type: .infoDark)
        tutorialButton.frame = CGRect(x: self.view.frame.size.width - 20, y: 42, width: 20, height: 20)
        tutorialButton.addTarget(self, action: #selector(self.showTutorial), for: .touchUpInside)
        
        view.addSubview(tutorialButton)

希望对您有所帮助!