2016-08-11 7 views
0

UIButtonが選択されているときに問題が発生しました。コードは、スウィフト3である:それはradioButton.isSelected = trueによって選択されている場合UIButton選択した画像が機能しない

let radioButton = UIButton(type: .system) 
radioButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40) 
radioButton.setImage(UIImage(named: "buttonUnchecked"), for: .normal) 
radioButton.setImage(UIImage(named: "buttonChecked"), for: .selected) 
radioButton.setTitle("This is some text.", for: .normal) 
radioButton.sizeToFit() 

、全く画像が示されていません。

+2

の? –

+0

あなたのビューにradioButtonを追加しましたか? – penatheboss

答えて

0

あなたはこの

radioButton.addTarget(self, action: "addMethodName", forControlEvents: UIControlEvents.TouchUpInside) 

、あなたはこのような画像を変更する方法のようにターゲットを追加しようとすることができます:

radioButton.setImage(UIImage(named: "buttonUnchecked"), for: .normal) 
+0

はい、これは私が「選択された」状態を使用しないと可能になります。しかし、私はボタンラベルのデフォルトの色の背景のためにそれを使用したいと思います。 もう少しアイデアはありますか? ;) – Heiko

0

あなたはおそらく、カスタムボタン式の代わりに、システムタイプを使用する必要があります。これを試してみてください:

let radioButton = UIButton(type: .Custom) 

代わりにあなたがイメージが `buttonChecked`はタイプミスせずに存在していることを確認しました

let radioButton = UIButton(type: .system) 
関連する問題