12882

How do I remove UITabBarItem selectionImage padding?

Question:

I am setting the SelectionIndicatorImage as a stretchable image (to accommodate for various device widths) via UITabBar appearance:

UIImage *selectedImage = [[UIImage imageNamed:@"SelectedTab"] stretchableImageWithLeftCapWidth:0 topCapHeight:0]; [[UITabBar appearance] setSelectionIndicatorImage:selectedImage];

As a result I get a 2pt padding on the screen edges. I am basically trying to use selection indicator as a background for currently selected UITabBarItem.

<a href="http://i.stack.imgur.com/qFHEk.png" rel="nofollow">http://i.stack.imgur.com/qFHEk.png</a>

What would be an easy way to solve this?

Answer1:

If you want to use UITabBarController in your app with selection and unselection feature then you should this code

[[self.tabBarController tabBar] setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bg_tabBar" ofType:@"png"]]]; [[self.tabBarController tabBar] setSelectionIndicatorImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bg_tabItem_selected" ofType:@"png"]]]; NSArray *arrTabItems = [[self.tabBarController tabBar] items]; UITabBarItem *tabBarItem = [arrTabItems objectAtIndex:0]; [tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"img_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"img_unselected.png"]]; [tabBarItem setImageInsets:UIEdgeInsetsMake(5, 5, 0, 5)];

Answer2:

I've tried something similar. Ended up this way:

Subclass UITabBarController and add property:

@property(nonatomic, readonly) UIImageView *imageView;

Setup your image as you want:

- (void)viewDidLoad { [super viewDidLoad]; _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbaritem"]]; [self.tabBar addSubview:self.imageView]; }

Place your subview in correct position:

- (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; // get all UITabBarButton views NSMutableArray *tabViews = [NSMutableArray new]; for (UIView *view in self.tabBar.subviews) { if ([view isKindOfClass:[UIControl class]]) { [tabViews addObject:[NSValue valueWithCGRect:view.frame]]; } } // sort them from left to right NSArray *sortedArray = [tabViews sortedArrayUsingComparator:^NSComparisonResult(NSValue *firstValue, NSValue *secondValue) { CGRect firstRect = [firstValue CGRectValue]; CGRect secondRect = [secondValue CGRectValue]; return CGRectGetMinX(firstRect) > CGRectGetMinX(secondRect); }]; // place your imageView on selected index button frame CGRect frame = self.tabBar.bounds; CGSize imageSize = CGSizeMake(CGRectGetWidth(frame) / self.tabBar.items.count, self.imageView.image.size.height); CGRect selectedRect = sortedArray.count > self.selectedIndex ? [sortedArray[self.selectedIndex] CGRectValue] : CGRectZero; [self.imageView setFrame:CGRectIntegral(CGRectMake(CGRectGetMinX(selectedRect), CGRectGetMaxY(frame) - imageSize.height, CGRectGetWidth(selectedRect), imageSize.height))]; }

Then you only need to refresh layout on each tab change:

- (void)setSelectedIndex:(NSUInteger)selectedIndex { [super setSelectedIndex:selectedIndex]; [self.view setNeedsLayout]; } - (void)setSelectedViewController:(UIViewController *)selectedViewController { [super setSelectedViewController:selectedViewController]; [self.view setNeedsLayout]; } <hr />

Old answer how to remove padding (this is what I firstly understood as your question, I'm leaving it as a reference for other people).

In iOS 7+ UITabBar has following property:

/* Set the itemSpacing to a positive value to be used between tab bar items when they are positioned as a centered group. Default of 0 or values less than 0 will be interpreted as a system-defined spacing. */ @property(nonatomic) CGFloat itemSpacing NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;

This should enable you to get rid of padding between items.

Recommend

  • How to Custom Tab bar item text
  • Swift - How to change UITabBarItem badge font?
  • Tab bar without TabBarController - add View Controller for Tab bar item in storyboard
  • Can't access TabBarController from ImageView
  • Error in Compiling haskell .ll file with llvm backend
  • Visual C++ Runtime Library Linker Woes
  • How to call JSON webservice from background in iPhone
  • jquery sending data with escapes?
  • Spark Runs in Local but not in YARN
  • Custom jQuery Pseudo-selector Receiving Undefined Arguments
  • Getting rid of ANSI escape characters when viewing ri in (Mac)Vim
  • Changing Key Of An Property in Json
  • Unresolved external symbol (OpenGL and c++)
  • Angular bind object element to HTML
  • how would I constrain to non-function?
  • Keep playing a sound over and over again in Matlab?
  • How can I make consuming custom events on my classes optional?
  • Why String.ToString()? [duplicate]
  • Stanford-parser in Ruby does not create Preprocesser
  • How to store WindowsIdentity for use at a later, indeterminate time
  • Calling JS from an applet works in Firefox & Chrome but not Safari
  • Create JSON Request string using Javascript Overlay types in GWT
  • Why can't the compiler deduce template parameter from return type?
  • Manipulate Object literal: add property in certain order
  • How to mock springSecurityService in unit tests using grails
  • Ajax call always returning error 500 client side
  • ES6 in NodeJS: Arrow functions in object literal, what is the returned 'this' value? [dupl
  • C# Dictionary ContainsKey
  • Entity Framework load relation
  • Memory dump much smaller than available memory
  • angularjs ng-bind-html html input part missing
  • Xamarin iOS debugger not hitting breakpoints
  • Encode string to match encoded form field name in PHP POST array
  • Get all existing pointers to an object
  • Debugging Android native application using ndk-gdb
  • List comprehension with if conditional to get list of files of a specific type
  • How to load gif image while ajax content is loading and javascript [duplicate]
  • Using a canvas object in a thread to do simple animations - Java
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • Binding checkboxes to object values in AngularJs