SetSelectionIndicatorImage has the wrong size for iphone 6 and iPhone 6+

I use the following method to set a selection indicator for a selected tab bar item. It works well for iPhone 4 / 4s / 5 / 5s, but not on iphone 6/6 +.

[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"activeshape.png"] ]; 

Any suggestion

+7
ios objective-c
source share
2 answers

EDIT: It seems like after all this solution should work, I am having problems with the cache

 UIImage *selTab = [[UIImage imageNamed:@"tabHighlight"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; CGSize tabSize = CGSizeMake(CGRectGetWidth(self.view.frame)/5, 49); UIGraphicsBeginImageContext(tabSize); [selTab drawInRect:CGRectMake(0, 0, tabSize.width, tabSize.height)]; UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // [self.tabBar setSelectionIndicatorImage:reSizeImage]; 

tabHiglight is tabHiglight png, I tested other sizes, but this seems to be the best fit. I divide the width frame by the number of elements that I have on the tabBar - in my case 5, then I create a new UIImage from the "correct" size and set it as selectionIndicatorImage .

+15
source share

Here is my auto- UITabBarController subclass of UITabBarController . Just provide an image and it will be configured on all known iPhones and iPads. It will also update the size whenever the feature collection changes, so it supports all the new features and the iPad rotation.

 import UIKit class TabBarController: UITabBarController { override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) updateSelectionIndicatorImage() } override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) { super.traitCollectionDidChange(previousTraitCollection) updateSelectionIndicatorImage() } func updateSelectionIndicatorImage() { let width = CGRectGetWidth(tabBar.bounds) > 420 ? 420 : CGRectGetWidth(tabBar.bounds) var selectionImage = UIImage(named: "TabSelectionIndicator") let tabSize = CGSizeMake(width/5, 49) UIGraphicsBeginImageContext(tabSize) selectionImage?.drawInRect(CGRectMake(0, 0, tabSize.width, tabSize.height)) selectionImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() tabBar.selectionIndicatorImage = selectionImage } } 
+1
source share

All Articles