AleksandarNS
AleksandarNS

Reputation: 265

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.

Upvotes: 6

Views: 2008

Answers (2)

Dannie P
Dannie P

Reputation: 4622

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
                }
            }
        }
    }
}

Upvotes: 0

Kevin Mac
Kevin Mac

Reputation: 320

on didFinishLaunchingWithOptions put this code:

UITabBarController *tabBarContr = (UITabBarController *)self.window.rootViewController;

CGFloat tabBarItemWidth = self.window.rootViewController.view.bounds.size.width / tabBarContr.tabBar.items.count;
CGFloat tabBarItemHeight = CGRectGetHeight(tabBarContr.tabBar.frame);

UIView *selectedBottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tabBarItemWidth, tabBarItemHeight)];

 CAGradientLayer *gradient = [CAGradientLayer layer];

gradient.colors = [NSArray arrayWithObjects:
                   (id)[UIColor blueButton].CGColor,
                   (id)[UIColor colorWithRed:0.08 green:0.54 blue:1 alpha:1].CGColor,
                   nil];

gradient.frame = selectedBottomView.bounds;

[selectedBottomView.layer insertSublayer:gradient atIndex:0];

UIGraphicsBeginImageContext(selectedBottomView.bounds.size);
[selectedBottomView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

tabBarContr.tabBar.selectionIndicatorImage = image;
tabBarContr.tabBar.translucent = YES;
tabBarContr.tabBar.tintColor = [UIColor whiteColor];

Note:take imageview instead of gradient

Upvotes: 2

Related Questions