不要用Appearance proxy設定UIView的tintColor

大招至勝發表於2016-08-30

在iOS7以後就不要用Appearance proxy來設定UIView的tintColor,否則會影響其它的tintColor.

最開始我不知道,就用這方法來設定了UITextView的tintColor

[[UITextView appearance] setTintColor:[UIColor blueColor]];

結果在設定UIBarButtonItem的tintColor的時候就沒有效果

UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
backItem.tintColor = [UIColor redColor];
self.navigationItem.leftBarButtonItem = backItem;

原因在UIAppearance.h中

Note for iOS7: On iOS7 the tintColor property has moved to UIView, and now has special inherited behavior described in UIView.h.
This inherited behavior can conflict with the appearance proxy, and therefore tintColor is now disallowed with the appearance proxy.

所有不要用Appearance proxy來設定UIView的tintColor。

相關文章