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.
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:
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With