können Sie das tun, ohne -insertSubview:atIndex
, weil eine neue UIView nicht benötigt wird. Sie können ein Design mit QuartzCore in jeder Ansicht anwenden (UITabBar und seine Subviews). Also wird der Hintergrund der UITabBar hinzugefügt, wie ich here beschrieben habe.
Nun müssen wir das Bild auf jedem UITabBarItem anwenden, da es Hintergrund ist:
// UITabBar+CustomItem.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface UITabBar (CustomItem)
-(void)setSelectedItemBackground:(UIImage *)backgroundImage;
@end
Nun ist die .m-Datei:
// UITabBar+CustomItem.m
@implementation UITabBar (CustomItem)
#define kItemViewTag 445533 // <-- casual number
#define kItemViewOldTag 445599 // <-- casual number different from the above
-(void)setSelectedItemBackground:(UIImage *)backgroundImage {
UIView *oldView = [self viewWithTag:kImageViewItemTag];
oldView.layer.contents = nil; // <-- remove the previous background
oldView.tag = kItemViewOldTag; // <-- this will avoid problems
NSUInteger index = [self.items indexOfObject:self.selectedItem];
UIView *buttonView = [self.subviews objectAtIndex:index];
buttonView.tag = kItemViewTag;
buttonView.layer.contents = (id)backgroundImage.CGImage; // <-- add
// the new background
}
@end
Sie können auch die Farbe der ausgewählten Bilder ändern, als jemand, gemacht here. Aber was ich mich wundere ist: Kann ich die Farbe des ausgewählten Etiketts ändern? Die Antwort ist ja, wie weiter unten (die folgenden Werke auf ios 3.x/4.x nicht iOS5 +) beschrieben:
@implementation UITabBar (Custom)
#define kSelectedLabel 334499 // <-- casual number
-(void)changeCurrentSelectedLabelColor:(UIColor *)color {
UIView *labelOldView = [self viewWithTag:kSelectedLabel];
[labelOldView removeFromSuperview];
NSString *selectedText = self.selectedItem.title;
for(UIView *subview in self.subviews) {
if ([NSStringFromClass([subview class])
isEqualToString:@"UITabBarButton"]) {
for(UIView *itemSubview in subview.subviews) {
if ([itemSubview isKindOfClass:[UILabel class]]) {
UILabel *itemLabel = (UILabel *)itemSubview;
if([itemLabel.text isEqualToString:selectedText]) {
UILabel *selectedLabel = [[UILabel alloc]
initWithFrame:itemLabel.bounds];
selectedLabel.text = itemLabel.text;
selectedLabel.textColor = color;
selectedLabel.font = itemLabel.font;
selectedLabel.tag = kSelectedLabel;
selectedLabel.backgroundColor = [UIColor clearColor];
[itemSubview addSubview:selectedLabel];
[selectedLabel release];
}
}
}
}
}
}
@end
Es gab unten einige Warnungen dieser Methoden, die in Bezug auf Apples Regeln etwas riskant sind ... können Sie bestätigen, dass alle von ihnen erfolgreich vom AppStore genehmigt wurden? – Chazbot
Mögliches Duplikat: http://stackoverflow.com/questions/1355480/preventing-a-uitabbar-from-applying-a-gradient-to-its-icon-images/ – rpetrich