我正在更改 setSelectionIndicatorImage
,当我在 iOS 8 上运行应用程序时,我得到图像和 tabBar
的常规宽度之间的间距。有没有一种方法可以使标签栏的高度与 setSelectionIndicatorImage
相匹配?此外,我在第一个选项卡的左侧和最后一个选项卡的右侧获得了几个像素的边距,并且在选择选项卡时我也需要用图像覆盖它。
最佳答案
在 didFinishLaunchingWithOptions 上放置这段代码:
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];
注意:取imageview而不是gradient
关于ios - TabBar 选择的指示器图像在 Objective C 中没有占据整个 tabBar 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26675875/