2017-10-12 20 views
0

私は選択されたトグルを持つ透明な画像を持つUIButtonを持っています。 現在の設定では、システムボタンタイプを使用しています。これは、ボタンをうまく逆転させるためです。しかし、この反転では、罫線のある画像が得られ、この画像をボタン全体に塗りたいと思っています。これはどのように達成できますか?私はシステムから透明マスクを保ちたいと思います。下の画像でiOS - UIButtonシステム選択された画像

Here is the repository.

選択したシステムの状態がどのように見えるかです。

enter image description here

+0

をテンプレートに画像のオプションをレンダリング設定する必要がありますもう一つはそこにあります? –

+0

iOSが選択したボタンを反転させてボタン全体の枠を塗りつぶす丸みのある矩形が必要です。 –

+0

sry、それは私には明らかではありません。 –

答えて

0

UIButtonのサブクラスを作成し、あなたの要件この

同様

とビューコントローラドゥにおけるに従って、実施例

class CustomButton: UIButton { 

    /* 
    // Only override draw() if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    override func draw(_ rect: CGRect) { 
     // Drawing code 
    } 
    */ 




    override var isSelected: Bool { 


     didSet { 
      if isSelected { 
       self.backgroundColor = UIColor.blue 
       self.tintColor = UIColor.white 
      } else { 
       self.backgroundColor = .white 
       self.tintColor = UIColor.red 
      } 

     } 
    } 

} 

色の変更についてisSelected

を上書き

class ViewController: UIViewController { 

@IBOutlet weak var button: CustomButton! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    button.isSelected = false 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
@IBAction func btnTapped(_ sender: Any) { 
    button.isSelected = !button.isSelected 
} 

}

そして、あなたはあなたがイメージとボタンから何をしたいですTemplate Image

+0

白い背景の場合、これは機能しますが、画像の背景はどうですか? –

+0

あなたはそれらを白にして、実行時に色を適用する必要があります –

関連する問題