Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS:UITabbar item click again and again it is reducing the UITabbar button item size in IOS 7

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.

like image 674
user3840618 Avatar asked Jul 24 '14 09:07

user3840618


2 Answers

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.

like image 86
staticVoidMan Avatar answered Sep 17 '22 02:09

staticVoidMan


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
like image 23
Adrian Avatar answered Sep 19 '22 02:09

Adrian