user1747629
user1747629

Reputation: 51

Tab bar items to have different width

I am creating an iPhone app where i need to show 5 tab bar items . My requirement is I want first tab bar item to have smaller width than other 4 . is it possible and how to go about doing this. Please suggest .

Upvotes: 3

Views: 2565

Answers (3)

Rob Jones
Rob Jones

Reputation: 4985

I don't think it's possible to do this. UITabBar and UITabBarItem are two of the least customizable classes in UIKit. UITabBarItem does not even inherit from UIView.

I needed to something similar and ended up rolling my own TabBarController, TabBar and TabBarItem classes. If you only need the basic tab bar functionality it's not too much work. If you want the fancy new iOS 7 transitions it will be a lot more work.

Upvotes: 2

Mayank Jain
Mayank Jain

Reputation: 5754

Create a subclass from UITabBarController and write this code in viewDidLoad.

-(void)viewDidLoad
{
[super viewDidLoad];

[self addCustomElements];

}

-(void)addCustomElements
{
 // Backgroun
UIImageView* bgView = Your backgroun image for tab bar;
bgView.frame = CGRectMake(0, self.view.frame.size.height-50, 320, 50);




[self.view addSubview:bgView];



// Initialise our two images
UIImage *btnImage = default image for your tab item;
UIImage *btnImageSelected = selected image for your tab item;

tab1 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
tab1.frame = // Set the frame (size and position) of the button)
[tab1 setImage:btnImage forState:UIControlStateNormal];
  [btnMore setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[tab1 setTitle:@"title for 1st button" forState:UIControlStateNormal];

[tab1 setTag:0];

// Add my new buttons to the view
[self.view addSubview:tab1];


//repeat same for remaining button

// Setup event handlers so that the buttonClicked method will respond to the touch up inside event.
[tab1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];




 }

set this subclass to your tab bar controller.

Upvotes: -1

Gaurav
Gaurav

Reputation: 309

Please check it out custom tab bar: https://github.com/aalittle/ALCustomTabBarController

In this sample, please go to TabBarView.xib file, you can change all the tabs buttons width and height according to your requirement.

Hope, It will may helpful,

:)

Upvotes: 0

Related Questions