Reputation: 987
I am implementing a UITabBar
within another UITabBar
. My problem is that, the Second TabBar width remains constant regardless of the screen size. This stands out a lot in the bigger screens. I'm attaching a screenshot to make you understand better. Selection is indicated using a blue background
Here's the code:
GRect rect = CGRectMake(0, 0, self.tabBar.frame.size.width/2, self.tabBar.frame.size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,
[[UIColor colorWithRed:102.0/255.0 green:197.0/255.0 blue:234.0/255.0 alpha:1.0] CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.tabBar.selectionIndicatorImage = img;
The screenshots from iPhone6 Plus
Upvotes: 7
Views: 670
Reputation: 262
Thank you for snippet of a highlight buttons.
Did you want something like this?
Portrait orientation:
Code of my ViewController:
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITabBar *tabBar;
@property (weak, nonatomic) IBOutlet UITabBar *topTabBar;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[UITabBar appearance] setTintColor:[UIColor redColor]];
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:20]} forState:UIControlStateNormal];
}
- (void)viewDidLayoutSubviews {
[self highlightTabBarItems:self.tabBar];
[self highlightTabBarItems:self.topTabBar];
}
- (void)highlightTabBarItems:(UITabBar*)currentTabBar {
CGFloat highlightedWidth = self.view.frame.size.width/currentTabBar.items.count;
[currentTabBar setItemWidth:highlightedWidth];
CGRect rect = CGRectMake(0, 0, highlightedWidth, currentTabBar.frame.size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:102.0/255.0 green:197.0/255.0 blue:234.0/255.0 alpha:1.0] CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
currentTabBar.selectionIndicatorImage = img;
}
@end
Upvotes: 5