Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust iOS TabBar item title to the centre of it

Tags:

ios

//TabBarController code:

self.delegate=self;
self.tabBarController.tabBar.delegate=self;

CGRect viewFrame=self.tabBar.frame;
viewFrame.origin.y -=0;![enter image description here][1]
viewFrame.origin.x -=0;
viewFrame.size.height=30;
self.tabBar.frame=viewFrame;

firstViewController = [[FirstViewController alloc] initWithNibName:nil bundle:NULL];
secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:NULL];

NSArray *twoViewControllers = [[NSArray alloc] initWithObjects:
                                       self.firstViewController, self.secondViewController, nil];

self.viewControllers=twoViewControllers;



//    ====================================================
//    
//    FirstViewController code in initWithNibName: 
//    
//    To set the title of the first tabbar item:


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"Article view";
        NSLog(@"count = %d",[self.tabBarController.tabBar.items count]);

    }
    return self;
}

//How can i make the first tabbar item title "Article View" to the Center without adding any //image to the tabbaritem ?

// similar to the below tabbar items screenshot.

  [1]: http://i.stack.imgur.com/xBpVH.png


Thanks in advance.
like image 354
Vivek Coolboy Avatar asked Dec 09 '22 17:12

Vivek Coolboy


1 Answers

Replace the initWithNibName method

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"Article view";
        self.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
        NSLog(@"count = %d",[self.tabBarController.tabBar.items count]);
    }
    return self;
}

self.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0); this line here adjusts the position of the image for the tabBarItem in the manner :

Shift the image in x-direction '+5' and in y-direction '-5' from the default position.

Play with UIEdgeInsetsMake and have fun. Cheers.

like image 105
devluv Avatar answered Dec 28 '22 23:12

devluv