2015-01-09 2 views
2

프로그래밍 방식으로 버튼을 만들고 있는데,이 버튼의 UIButtonType을 설정하는 방법을 알고 싶습니다. 내 버튼을 만드는 오전 방법 버튼 유형 설정

은 다음과 같습니다

headerPlusButon.buttonType = UIButtonType.ContactAdd 
: 당신이 제목의 텍스트를 변경 것이라고 분명히 문제가 해결되지 않는 방법 같은 더 "setButtonTypeFor ..."방법이 없습니다

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

+0

단추 유형을 만든 후에는 설정할 수 없습니다. 버튼을 만들 때 싫어한다. – Crazyrems

+0

좋아, Abhishek Sharma가 해결책을 갖게했다. UIButton으로 버튼 = UIButton.buttonWithType (UIButtonType.System)을 사용한다. –

답변

8
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 2은 아래 라인으로 UIButton 선언을 대체하십시오.

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

buttonWithType을 사용할 수 없다. –

+0

@yestayMuratov 덕분에 나는'code' –

1

여기에 내 코드

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)
내가 당신을 도울 수 있기를 바랍니다

입니다!