2017-02-24 123 views
0
@IBAction func btnPrimaryAction(_ sender: Any) { 
     btnPrimary.setImage(UIImage(named : "checkbox_on.png") , for: UIControlState.normal) 
     btnSecondary.setImage(UIImage(named : "checkbox_off.png"), for: UIControlState.normal) 
     btnNewAdd.setImage(UIImage(named : "checkbox_off.png") , for: UIControlState.normal) 
     payType = 1 
    } 

我正在使用此代碼來更改按鈕圖像時單擊按鈕。但按鈕的圖像消失的iOS 10.單擊按鈕時按鈕與圖像,圖像消失點擊按鈕時iOS 10

請幫我這個 謝謝你是進步

+0

你確定按鈕的狀態。中性?因爲我可以看到你只是爲一個狀態設置圖像。因此,如果狀態是其他任何東西,你的圖像將是空的 –

+0

狀態總是正常的即時消息不改變狀態...它在一個視圖控制器上工作相同的代碼一切都沒有改變,但沒有在其他視圖控制器上工作 –

回答

0

我認爲這是使用UIButton國家有效的方式。 UIButton有多個狀態,如UIControlState.normal,UIControlState.selected,UIControlState.highlighted等。 您可以使用UIButton的UIControlState輕鬆解決您的問題。 首先你需要像這樣設置圖像到你的按鈕。 在viewDidLoad方法。

// if you have add the "checkbox_off.png and checkbox_on.png" in Images.xcassets then get the image you need to write as below 
//UIImage(named : "checkbox_off") no need to add extention like ".png" 

btnPrimary.setImage(UIImage(named : "checkbox_off.png") , for: UIControlState.normal) 
btnSecondary.setImage(UIImage(named : "checkbox_off.png"), for: UIControlState.normal) 
btnNewAdd.setImage(UIImage(named : "checkbox_off.png") , for: UIControlState.normal) 

// Selected  
btnPrimary.setImage(UIImage(named : "checkbox_on.png") , for: UIControlState.selected) 
btnSecondary.setImage(UIImage(named : "checkbox_on.png"), for: UIControlState.selected) 
btnNewAdd.setImage(UIImage(named : "checkbox_on.png") , for: UIControlState.selected) 

現在在你的函數只是改變了按鈕的狀態

@IBAction func btnPrimaryAction(_ sender: Any) { 
    btnPrimary.selected = true 
    btnSecondary.selected = false 
    btnNewAdd.selected = false 
    payType = 1 
} 
+0

我試過這個,但沒有爲我工作。 idk發生了什麼......相同的代碼在一個控制器中工作,並且不能在其他控制器中工作 –