UITabBar selection indicator image does not accept the entire tabBar height size in ObjectiveC

I change setSelectionIndicatorImage , and when I launch the application on iOS 8, I get the gap between the image and the regular width of the tabBar . Is there a way that I can match the height of the tab bar with setSelectionIndicatorImage ? In addition, I get a box with several pixels on the left side of the first tab and the right side of the last tab, and I need to cover this image also when selecting the tab.

+6
source share
2 answers

This will help you adjust the image of the indicator, but you need to. You just set the Tab Bar class for 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 } } } } } 
0
source

on didFinishLaunchingWithOptions puts 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 an image instead of a gradient

+2
source

Source: https://habr.com/ru/post/977537/


All Articles