2016-03-03 114 views
1

我已将子类型TabBarController设置为渐变背景。但它似乎并不奏效。没有显示。我是通过使用Hue框架tabBar中的渐变背景

let tabBarGradient = [UIColor.redColor(), UIColor.blackColor()].gradient { gradient in 
     gradient.frame = self.tabBar.frame 
     gradient.startPoint = CGPointMake(0,0.5) 
     gradient.endPoint = CGPointMake(1,0.5) 

     return gradient 
    } 

    self.tabBar.layer.addSublayer(tabBarGradient) 
+0

没有我不忘记设置的颜色? –

回答

5

在CustomTabBarController的方式,写这样的代码:

import UIKit 

class GradientTabBarController: UITabBarController { 

    let layerGradient = CAGradientLayer() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     layerGradient.colors = [UIColor.redColor().CGColor, UIColor.yellowColor().CGColor] 
     layerGradient.startPoint = CGPoint(x: 0, y: 0.5) 
     layerGradient.endPoint = CGPoint(x: 1, y: 0.5) 
     layerGradient.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height) 
     self.tabBar.layer.addSublayer(layerGradient) 
    } 
} 

你可以看到梯度你的TabBar这样的:

enter image description here

而故事板则将CustomTabBarController分配给故事板中的TabBarController。

enter image description here

1

要扩大Khuong的答案,因为它使用的是view.Frame设置宽度和高度为layerGradient.Frame上面不会使用Y梯度工作。 view是容器视图,并且大于标签栏。相反设定时,layerGradient.Frame使用tabBar.Frame

layerGradient.frame = CGRect(x: 0, y: 0, width: tabBar.bounds.width,height: tabBar.bounds.height)