0

I created a TabBar for all iphone but in iphone 5 showing properly but in iphone 6 not showing properly image iPhone 6 showing like that i want proper spacing between tabbaritem and left and right

enter image description here

in Iphone 5 showing properly

enter image description here

I am using this code

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

UITabBar *tabBar = self.tabBar;

[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"footer_bg"]];
[[UITabBar appearance] setTintColor:[UIColor clearColor]];
[[UITabBar appearance] setTintColor:[UIColor clearColor]];

self.delegate=self;

self.imgTab1= getImgViewForTab(CGRectMake(0, 6, 80, 39), @"listings_active.png");
self.imgTab1.autoresizesSubviews = YES;
self.imgTab1.autoresizingMask = LEFT_MARGIN & TOP_MARGIN & BOTTOM_MARGIN & F_WIDTH & F_HEIGHT;
[tabBar addSubview:self.imgTab1];

self.imgTab2= getImgViewForTab(CGRectMake(80, 6, 80, 39), @"bids.png");
self.imgTab2.autoresizesSubviews = YES;
self.imgTab2.autoresizingMask = TOP_MARGIN & BOTTOM_MARGIN & F_WIDTH & F_HEIGHT;
[tabBar addSubview:self.imgTab2];

self.imgTab3= getImgViewForTab(CGRectMake(160, 6, 80, 39), @"reservations.png");
self.imgTab3.autoresizesSubviews = YES;
self.imgTab3.autoresizingMask = TOP_MARGIN & BOTTOM_MARGIN & F_WIDTH & F_HEIGHT;
[tabBar addSubview:self.imgTab3];

self.imgTab4= getImgViewForTab(CGRectMake(240, 6, 80, 39), @"settings.png");
self.imgTab4.autoresizesSubviews = YES;
self.imgTab4.autoresizingMask = RIGHT_MARGIN & TOP_MARGIN & BOTTOM_MARGIN & F_WIDTH & F_HEIGHT;
[tabBar addSubview:self.imgTab4];

[self setSelectedTabImage:tabbarindexG];
}
Kishore Suthar
  • 2,943
  • 4
  • 26
  • 44
  • Place code for getImgViewForTab method. Something wrong seems in that method. It seems like, you are hard coding image placements in tabbar. – Mrunal Nov 24 '14 at 07:05
  • inline static UIImageView *getImgViewForTab(CGRect frame, NSString *imgName) { UIImageView *imgtab = [[UIImageView alloc] initWithFrame:frame]; [imgtab setImage:[UIImage imageNamed:imgName]]; return imgtab; } – Kishore Suthar Nov 24 '14 at 07:12
  • possible duplicate http://stackoverflow.com/questions/26730544/uitabbar-in-xcode-6 – Vineesh TP Nov 24 '14 at 07:59

1 Answers1

4

It seems to me as a wrong approach to assign image to tabbar item. Please refer this code;

You have to specify the images for every tab item. The following code is working for me

    if ([[tabBarController.tabBar.items objectAtIndex:0] respondsToSelector:@selector(setFinishedSelectedImage:withFinishedUnselectedImage:)]) {

      [[tabBarController.tabBar.items objectAtIndex:0] setFinishedSelectedImage:[UIImage imageNamed:@"simg1.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"img1.png"]];
      [[tabBarController.tabBar.items objectAtIndex:1] setFinishedSelectedImage:[UIImage imageNamed:@"simg2.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"img2.png"]];

      // Do the same if there are more tabs

    }

Or use default setImage and setSelectedImage accessor methods:

// both at once
[initWithTitle:image:selectedImage:][1]

// only setImage   
 [[tabBarController.tabBar.items objectAtIndex:0] setImage: [UIImage imageNamed: @"tab_1_image"]];

// only setSelectedImage
 [[tabBarController.tabBar.items objectAtIndex:0] setSelectedImage: [UIImage imageNamed: @"tab_1_selected_image"]];

iOS API will automatically adjust frame and placement for image, there is no need to assign frame for image.

Hope this will solve your problem.

Mrunal
  • 13,982
  • 6
  • 52
  • 96
  • woking perfect and add my image name but not showing my images – Kishore Suthar Nov 24 '14 at 09:22
  • Check your tabbar icon size is as per Apple recommendation or not. https://developer.apple.com/library/ios/documentation/userexperience/conceptual/mobilehig/IconMatrix.html#//apple_ref/doc/uid/TP40006556-CH27-SW1 – Mrunal Nov 24 '14 at 12:19