「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では、テンプレート画像の色合い。
- WatchKit アプリの Asset Catalog に画像を追加し、Attributes Inspector でテンプレート画像としてレンダリングされる画像セットを設定する必要があります。iPhone アプリとは異なり、現時点では WatchKit 拡張機能のコードでテンプレートのレンダリングを設定することはできません。
- アプリのインターフェースビルダーのWKInterfaceImageで使用する画像を設定します。
- 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
テンプレート画像として使用すると、デフォルトで水色に着色されます。