为了完整起见,这是我的最终代码。再次感谢 MaxGabriel 的回答。
新文件,Objective-C 类别(UIView)
// UIView+UIMods.m
// ATB iPad
//
// Created by A Smith on 10/4/13.
//
//
#import "UIView+UIMods.h"
#import "Colors.h"
- (void)changeColorOfLabels:(int)hiContrast
{
if ([self isKindOfClass:[UILabel class]]) { // if we are currently in a UILabel view
/* change color */
UILabel *label = (UILabel *)self;
NSString *ver = [UIDevice currentDevice].systemVersion;
int majorVer = [ver integerValue];
if( hiContrast ){
if( ![label.superview isKindOfClass:[UIButton class]] ){
[label setTextColor:[UIColor blackColor]];
[label setBackgroundColor:[UIColor whiteColor]];
} else {
if( majorVer >= 7 ){
[label setTextColor:[Colors iOS7ButtonColor]];
}
}
} else {
if( ![label.superview isKindOfClass:[UIButton class]] ){
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[Colors darkBkgColor]];
} else {
if( majorVer >= 7 ){
[label setTextColor:[Colors iOS7ButtonColor]];
}
}
}
}
for (UIView *subview in self.subviews) {
if (![subview isKindOfClass:[UISegmentedControl class]] && ![subview.superview isKindOfClass:[UITextField class]] ) {
[subview changeColorOfLabels:hiContrast]; // only recurse down if it's the type of view we want to modify the label for
}
}
}
和 .h 文件:
@interface UIView (UIMods){
}
- (void)changeColorOfLabels:(int)hiContrast;
@end
它是这样称呼的,
UIView *aView = theView.view;
[aView changeColorOfLabels:globalHighContrast];
我看到的一个很好的建议是将 .h 文件导入添加到 .pch 文件中,这样这个添加在任何地方都可用,您不必在本地导入任何内容。
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "UIView+UIMods.h"
#endif