ObjecTips

Swift & Objective-C で iOS とか macOS とか

Xcode 6 での Storyboard の画像設定の不具合

Xcode 6.3 で検証
xcassets で Render As Template Image に設定したテンプレート画像を Storyboard 上で UIButton に設定してビルドした時に、iOS 8 だと正しく tintColor が反映されるけど iOS 7 だと tintColor が設定されないという問題がある。たぶん不具合。
UIImageView にいたっては iOS 8 でも tintColor が反映されない。

f:id:Koze:20150413062916p:plain f:id:Koze:20150413062920p:plain

対処方法としてはコードで画像を設定する。

    UIImage *image = [[UIImage imageNamed:@"circle"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    [self.button setImage:image forState:UIControlStateNormal];
    self.imageView.image = image;

上記コードで iOS 7 での問題は解消される。
でも iOS 8 の UIImageView では問題が解消されない。

f:id:Koze:20150413063413p:plain f:id:Koze:20150413063417p:plain

iOS 8 の UIImageView の問題を解消するには tintColor を設定してやる必要がある。
Storyboard 上で現在設定されている tintColor をそのまま設定しても変更が反映されないので、一度 tintColor を nil にしてから再設定する。
iOS 8 では tintColor を設定すれば画像は再設定してやる必要はない。

    UIColor *color = self.imageView.tintColor;
    self.imageView.tintColor = nil;
    self.imageView.tintColor = color;

これにて対応完了

f:id:Koze:20150413070021p:plain f:id:Koze:20150413070028p:plain

対応内容をまとめると以下になる。

iOS 7 iOS 8
UIButton コードで画像設定 対応不要
UIImageView コードで画像設定 コードでtintColor設定