TabBar 选择指示器图像在 Objective C 中没有占据 tabBar 高度的整个大小

Posted

技术标签:

【中文标题】TabBar 选择指示器图像在 Objective C 中没有占据 tabBar 高度的整个大小【英文标题】:UITabBar selection indicator image is not taking whole size of tabBar height in ObjectiveC 【发布时间】:2014-10-31 13:28:38 【问题描述】:

我正在更改 setSelectionIndicatorImage,当我在 ios 8 上运行应用程序时,图像和常规宽度之间的间距为 tabBar。有没有办法可以将标签栏的高度与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.

【问题讨论】:

请添加修改过的和想要的图片。同时添加您的代码。 还可以查看本教程。这可能会有所帮助。 blog.erikvdwal.nl/customizing-the-uitabbar-in-ios-5-and-ios-6 请参考选择/取消选择标签栏图像的正确图像尺寸。 developer.apple.com/library/ios/documentation/UserExperience/… 您是否能够删除第一个选项卡左侧和最后一个选项卡右侧的几个像素边距? 【参考方案1】:

在 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代替渐变

【讨论】:

【参考方案2】:

这应该可以帮助您根据需要自定义指示器图像。您只是在界面构建器中将 Tab Bar 的类设置为此类

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
                
            
        
    

【讨论】:

以上是关于TabBar 选择指示器图像在 Objective C 中没有占据 tabBar 高度的整个大小的主要内容,如果未能解决你的问题,请参考以下文章

Flutter 自定义TabBar指示器(indicator)实现秒杀UI样式

Flutter——TabBar组件(顶部Tab切换组件)

如何在 tabBar swift 中更改项目或图像颜色?

在 Swift/Objective-c 中按媒体类型自定义图像选择器

如何在 iOS 中完全自定义 TabBar

如何使用objective-c从数组中选择不同的图像