iOSとWatchKitで画像の色合いを変更するにはどうすればいいですか?質問する

iOSとWatchKitで画像の色合いを変更するにはどうすればいいですか?質問する

「theImageView」という UIImageView があり、UIImage は下の左側の黒いハートのように単色 (透明な背景) になっています。iOS 7 以降のナビゲーション バー アイコンで使用される色合いメソッドに従って、iOS 7 以降でこの画像の色合いをプログラムで変更するにはどうすればよいですか?

この方法は、Apple Watch アプリの WatchKit でも機能しますか?

ここに画像の説明を入力してください

ベストアンサー1

iOS
iOS アプリの場合、Swift 3、4、または 5 の場合:

theImageView.image = theImageView.image?.withRenderingMode(.alwaysTemplate)
theImageView.tintColor = UIColor.red

Swift 2の場合:

theImageView.image = theImageView.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
theImageView.tintColor = UIColor.redColor()

一方、最新の Objective-C ソリューションは次のとおりです。

theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[theImageView setTintColor:[UIColor redColor]];

Watchkit
Apple WatchアプリのWatchKitでは、テンプレート画像の色合い

  1. WatchKit アプリの Asset Catalog に画像を追加し、Attributes Inspector でテンプレート画像としてレンダリングされる画像セットを設定する必要があります。iPhone アプリとは異なり、現時点では WatchKit 拡張機能のコードでテンプレートのレンダリングを設定することはできません。
  2. アプリのインターフェースビルダーのWKInterfaceImageで使用する画像を設定します。
  3. WKInterfaceController に 'theImage' という WKInterfaceImage の IBOutlet を作成します...

Swift 3 または 4 で色合いを設定するには、次の手順を実行します。

theImage.setTintColor(UIColor.red)

スイフト2:

theImage.setTintColor(UIColor.redColor())

次に、Objective-C で色合いを設定します。

[self.theImage setTintColor:[UIColor redColor]];

テンプレート画像を使用し、色合いを適用しない場合は、WatchKit アプリのグローバル色合いが適用されます。グローバル色合いを設定していない場合は、theImageテンプレート画像として使用すると、デフォルトで水色に着色されます。

おすすめ記事