如果您仅针对iOS 5,则可以使用外观代理,该代理允许您在整个应用程序的某个点上自定义UI元素。
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html
如果您需要为此在旧的iOS”,那么有没有非常好的解决方案来做到这一点。这里描述了一种使用方法调配的方法。
http://samsoff.es/posts/customize-uikit-with-method-swizzling
但这并不在iOS 5的工作了。最好的方法是使用iOS 5的外观代理,以及类似老式iOS的Swizzling的解决方法。
编辑:
下面是一些代码,我发现使用外观代理,如果它是可用的和使用方法交叉混合,如果它不是。
if ([[UINavigationBar class] respondsToSelector:@selector(appearance)]) {
// iOS >= 5.0 -> Use Appearance API
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
}
else {
// iOS < 5.0 -> Use Method Swizzling
Method drawRectCustomBackground = class_getInstanceMethod([UINavigationBar class], @selector(drawRectCustomBackground:));
Method drawRect = class_getInstanceMethod([UINavigationBar class], @selector(drawRect:));
method_exchangeImplementations(drawRect, drawRectCustomBackground);
}
的drawRectCustomBackground
方法在类别上实现UINavigationBar
。
感谢您的详细回答,但我决定,我周围惹这些事情之前,我宁愿用重复的代码活在每一个视图控制器导航栏。我的意思是,它只有4个控制器和大约3条线,所以我可以忍受这一点.. – urz0r