2017-12-08 5 views
-1

differents uiButton에서 다른 색 (코드 별)을 적용하고 싶습니다. 2 개의 배열을 만드는 것으로 생각했습니다. 처음에는 uiButton을 사용하고, 다른 하나는 다른 색상을 사용합니다. 그 후, 내 버튼으로 첫 번째 루프를 만들고이 루프 내부에서 색상을 사용하여 다른 루프를 만듭니다 (임의의 결과). 마지막 단계는 버튼에서 색상을 적용하는 것입니다 ... 이 방법으로 효과가 있는지 알려주는 사람이 있습니다. 감사합니다.각기 다른 색을 적용하는 uibutton의 배열

답변

0

당신은 그냥있는 viewDidLoad에서 메소드를 호출하고 임의의 색상을 설정하려면

func createButtonsWithDifferentTitleColor(){ 
    // create array of different colors 
    let colorArray = [UIColor.red,UIColor.green,UIColor.blue,UIColor.brown,UIColor.purple] 

    for i in 0...colorArray.count-1 { 
     let button = UIButton(frame: CGRect(x: 0, y: i*55, width: 130, height: 50)) 
     //set different title colors for each button 
     button.setTitleColor(colorArray[i], for: .normal) 
     // set different title for button 
     button.setTitle("Button No \(i+1)", for: .normal) 
     // add all buttons to main view, so that they can be visible 
     view.addSubview(button) 

    } 

} 

를 작동하는 방법을 참조 코드를 작업 this.Its 시도 할 수 있습니다 당신은이

func createButtonsWithDifferentTitleColor(){ 
    // create array of different colors 
    let colorArray = [UIColor.red,UIColor.green,UIColor.blue,UIColor.brown,UIColor.purple] 

    for i in 0...colorArray.count-1 { 
     let button = UIButton(frame: CGRect(x: 0, y: i*55, width: 130, height: 50)) 
     // generate random index 
     let index = Int(arc4random_uniform(UInt32(colorArray.count))); 
     //set different title color for each button 
     button.setTitleColor(colorArray[index], for: .normal) 
     // set different title for button 
     button.setTitle("Button No \(i+1)", for: .normal) 
     // add all buttons to main view, so that they can be visible 
     view.addSubview(button) 

    } 

} 
처럼 어떤 일을 할 네드
+0

당신의 도움에 감사드립니다, 나는 이것과 다른 대답을 시도 할 것입니다. – Wills

+0

다시 도움을 주셔서 감사합니다. 이 솔루션을 사용하지는 않지만 광산 찾기에 영감을줍니다. 다시 한 번 감사드립니다. – Wills

0

this answer을 사용하면됩니다.

let numberOfButton = 10 
var buttons = [UIButton]() 

for _ in 0..<numberOfButton { 
    let button = UIButton() 
    button.setTitleColor(.random(), for: .normal) 
    buttons.append(button) 
} 
+0

모에 고마워요, 이걸 시도해보고 작동하는지 다시 알려 줄 것입니다. – Wills

관련 문제