5

I'm took the Tabbar viewcontroller in this ,I added the 5 item and .I given the image insects is (24,0,0,6). All button images are added in xib [under the Bar item -->image]Please help. Thanks.

user3840618
  • 63
  • 1
  • 7
  • 1
    i have answered a similar question here. seems like the same issue -- [iOS Tab Bar icons keep getting larger](http://stackoverflow.com/questions/23306963/ios-tab-bar-icons-keep-getting-larger) ... **basically due to unbalanced image insets** – staticVoidMan Jul 24 '14 at 09:12
  • Please see this link::http://stackoverflow.com/questions/22452582/ios7-tab-bar-custom-icon-height-height-reduces-until-icon-gets-invisible.My problem also same help me please. – user3840618 Jul 24 '14 at 09:38

2 Answers2

9

Adding to a similar answer here: iOS Tab Bar icons keep getting larger

Not sure if this is an iOS7 bug but I've noticed that image insets need to be balanced.

You have specified insets for top and right but:

  1. if you set a top inset, in order to balance it, you need to set the negative of it to the bottom inset
  2. if you set a right inset, in order to balance it, you need to set the negative of it to the left inset

So, instead of having image insets like (24,0,0,6), use balanced image insets such as UIEdgeInsetsMake(24,-6,-24,6)

Doing so should protect your tabBarItem image from getting whacked on every tap.


If this doesn't suit your requirements, then redesign your tabBarItem image so you can have balance insets or... no insets at all.

Community
  • 1
  • 1
staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
  • If i put the same sizes, which you given ,the tabbar item is large and it's going to out of screen.If you din't mind just see this link::http://stackoverflow.com/questions/22452582/ios7-tab-bar-custom-icon-height-height-reduces-until-icon-gets-invisible.me also facing the same problem.Thanks – user3840618 Jul 24 '14 at 10:12
  • @user3840618 : Basically you want to push the image from the top and right but with imbalanced image insets, you're gonna go wrong everytime. Anyways... What are the image dimensions? I think you'd be better off if you edit the image and align it properly in an image editor and then, and only then, use it in your app. – staticVoidMan Jul 24 '14 at 17:58
  • what's the point of doing this. By doing this, you won't change anything so it's better to leave it with 0,0,0,0 – Next Developer Sep 07 '17 at 20:03
  • This bug fixed at iOS11 – Tà Truhoada Nov 16 '17 at 06:16
  • 1
    I am still seeing this on iOS 11. – de. Nov 17 '17 at 10:39
  • @de. I came up with a workaround that gets the job done. – Adrian May 29 '18 at 16:07
1

Here's the workaround for a bug I've encountered with UITabBarController's UITabBar. If I tap a UITabBarItem once after it's selected, the icon shrinks. What I'd like to do is disable touches. UITabBarItem only has a setting for isEnabled, which grays it out if I set it to false...not what I was looking for.

I used a derivative of this answer to figure it out. With a UITabBarController with 3 tabs, printing tabBarController.subviews, I saw 3 UITabBarButtons and a UIBarBackground. The origin of UIBarBackground's frame was always (0, 0), putting it at the front of the sorted array, so I really don't need to know what the subview is, just "where it is" and whether it will always be there. The UIBarBackground is always going to be at the front of an array of tabBarController.subviews sorted by frame.minX, so I just need to remove it from the front.

Solution

Here's what the extension looks like:

extension UITabBarController {
    var buttonViews: [UIView] {
        var tabBarButtons = tabBar.subviews.sorted(by: {$0.frame.minX < $1.frame.minX})
        tabBarButtons.removeFirst()
        return tabBarButtons
    }
}

I also created a struct in my Constants file, so I don't have to remember tab names:

struct TabBarItem {
    static let firstTab = 0
    static let secondTab = 1
    static let thirdTab = 2
}

...and finally, where to use it:

In viewDidAppear (NOT viewDidLoad), add the following line to disable the UITabBarItem that you don't want to disable, but not gray out:

tabBarController?.buttonViews[TabBarItem.firstTab].isUserInteractionEnabled = false

In viewWillDisappear, re-enable the tab, as follows:

tabBarController?.buttonViews[TabBarItem.firstTab].isUserInteractionEnabled = true
Community
  • 1
  • 1
Adrian
  • 16,233
  • 18
  • 112
  • 180
  • +1. Ended up using your solution as I needed the image to have a larger size and balancing the image insets wasn't giving the desired results – batman Aug 26 '19 at 07:49