Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITabBar width not increasing with screen size

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 First TabBar on Top Second on the bottom

Second Tab in Second TabBar Selected

Third tab Selected in Second TabBar Controller

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

Second Tab Of First TabBar selected(not part of the question, just to show the full picture)

like image 431
Sidharth J Dev Avatar asked Oct 23 '15 10:10

Sidharth J Dev


1 Answers

Thank you for snippet of a highlight buttons.
Did you want something like this?
Portrait orientation:

enter image description here


Landscape orientation:

enter image description here

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
like image 118
user3820674 Avatar answered Sep 28 '22 13:09

user3820674