Edit the title of the iOS TabBar element to the center of it

//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 title of the "View article" title element in the Center without adding any // image to the tabbaritem?

// similar to the screenshot below.

  [1]: http://i.stack.imgur.com/xBpVH.png Thanks in advance. 
+4
source share
1 answer

Replace 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 tabBarItem as follows:

Move the image in the "+5" direction along the x axis and in the "-5" direction in the default direction.

Play with UIEdgeInsetsMake and have fun. Greetings.

+14
source

All Articles