【问题标题】:UITabBar selection indicator image is not taking whole size of tabBar height in ObjectiveCTabBar 选择指示器图像在 Objective C 中没有占据 tabBar 高度的整个大小
【发布时间】: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.

【问题讨论】:

标签: ios objective-c uitabbar uitabbaritem


【解决方案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
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 2016-07-10
      • 2020-09-24
      • 1970-01-01
      • 2017-10-31
      • 2023-01-19
      • 2013-08-06
      相关资源
      最近更新 更多