Search code examples
iosobjective-cuitabbaruitabbaritem

UITabBar selection indicator image is not taking whole size of tabBar height in ObjectiveC


I am changing setSelectionIndicatorImage and when I run app on iOS 8, I get spacing between image and regular width of tabBar. Is there a way that I can match height of tab bar with setSelectionIndicatorImage? Also I get margin of few pixels on left side of first tab and right side of last tab, and I need to cover that with image too when tab is selected.


Solution

  • This should help you customise the indicator image however you need. You just set class of Tab Bar to this class in the interface builder

    class MyCustomTabBar: UITabBar
    {
        var didInit = false
        override func layoutSubviews()
        {
            super.layoutSubviews()
    
            if didInit == false
            {
                didInit = true
                for subview in subviews {
                    // can't hookup to subviews, so do layer.sublayers
                    subview.layer.addObserver(self, forKeyPath: "sublayers", options: .New, context: nil)
                }
            }
        }
    
        deinit
        {
            for subview in subviews
            {
                subview.layer.removeObserver(self, forKeyPath: "sublayers")
            }
        }
    
        override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>)
        {
            // layer.delegate is usually the parent view
            if let l = object as? CALayer, tbButton = l.delegate as? UIView where tbButton.window != nil
            {
                for v in tbButton.subviews
                {
                    if String(v.dynamicType) == "UITabBarSelectionIndicatorView" {
                        // do whatever needed with v, this is your indicator view
                    }
                }
            }
        }
    }