Ответ 1
Вот решение вашей проблемы... Я на самом деле не делал этого... Я делал что-то еще, но следующий код вам очень поможет... Сначала я расскажу вам, что я сделал...
-
Я сделал категорию
UITabbar
и реализовал следующий метод в том, что- (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur { CGColorRef cgColor = [color CGColor]; CGColorRef cgShadowColor = [shadowColor CGColor]; for (UITabBarItem *item in [self items]) { if ([item respondsToSelector:@selector(selectedImage)] && [item respondsToSelector:@selector(setSelectedImage:)] && [item respondsToSelector:@selector(_updateView)]) { CGRect contextRect; contextRect.origin.x = 0.0f; contextRect.origin.y = 0.0f; //instead of following line you can give our own desired size of contextRect. //just change the method parameters and include a parameter of desired size in it. // and this desired size would be the tabbarbutton size...so you will pass the size of // you tabbarbutton here...because on the back of image there is a tabbarbutton and if // set the image of button size it will occupy whole the are of button. contextRect.size = desired size //[[item selectedImage] size]; // Retrieve source image and begin image context UIImage *itemImage = [item image]; CGSize itemImageSize = [itemImage size]; CGPoint itemImagePosition; itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2); itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2); UIGraphicsBeginImageContext(contextRect.size); CGContextRef c = UIGraphicsGetCurrentContext(); // Setup shadow CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor); // Setup transparency layer and clip to mask CGContextBeginTransparencyLayer(c, NULL); CGContextScaleCTM(c, 1.0, -1.0); CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [itemImage CGImage]); // Fill and end the transparency layer CGContextSetFillColorWithColor(c, cgColor); contextRect.size.height = -contextRect.size.height; CGContextFillRect(c, contextRect); CGContextEndTransparencyLayer(c); // Set selected image and end context [item setSelectedImage:UIGraphicsGetImageFromCurrentImageContext()]; UIGraphicsEndImageContext(); // Update the view [item _updateView]; } } }
Теперь я вызвал вышеупомянутый метод в моем пользовательском классе UITabbarController... Я переопределяю метод
-(void)setSelectedIndex:(NSUInteger)selectedIndex
и сделал следующее в этом методе.-(void)setSelectedIndex:(NSUInteger)selectedIndex { self.selectedViewController = [self.viewControllers objectAtIndex:selectedIndex]; NSLog(@"selectedIndex:%d, totalCount:%d",selectedIndex,[self.tabBar.subviews count]); for (uint i=1; i < [self.tabBar.subviews count]; i++) { UIView *view = [self.tabBar.subviews objectAtIndex:i]; NSLog(@"class:%@",NSStringFromClass([view class])); if ([NSStringFromClass([view class]) isEqualToString:@"UITabBarButton"]) { //view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, self.tabBar.frame.size.height); NSLog(@"selectedIndex:%d,i:%d",self.selectedIndex,i); if (self.selectedIndex+1==i) { [self.tabBar recolorItemsWithColor:[UIColor whiteColor] shadowColor:[UIColor blackColor] shadowOffset:view.frame.size shadowBlur:0.5]; } } } }
Вы можете оптимизировать код, чтобы избежать создания catagory или подкласса... но для этого вам, должно быть, нужно взять Objective C. В случае любой проблемы вы можете мне рассказать. Приветствия