2017-03-04 101 views
0

阴影出现在导航栏的顶部。无论如何移动栏下的阴影或添加一些代码,以便它不会使栏变色?阴影变色导航栏

这是我在我的导航控制器使用的代码:

import UIKit 

    class navigationController:UINavigationController, UIViewControllerTransitioningDelegate{ 


    override func viewDidLoad() 
    { 
     var appblue = UIColor(red: 109/255, green: 208/255, blue:247/255, alpha: 1.0) 
     self.navigationBar.barTintColor = appblue 
     self.navigationBar.layer.masksToBounds = false 
     self.navigationBar.layer.shadowColor = UIColor.black.cgColor 
     self.navigationBar.layer.shadowOpacity = 0.7 
     self.navigationBar.layer.shadowOffset = CGSize(width: 0, height: 2.0) 
     self.navigationBar.layer.shadowRadius = 4 
    } 
} 

,这是什么样子:

enter image description here

当它应该是这种颜色:

enter image description here

A ny的建议?

这是我尝试使它看起来像什么(照片逛过图片): (最上面一栏底部阴影,同时保留第二条颜色) enter image description here

+0

在你的VC中嵌入导航控制器 –

+0

你的代码也正确无误,只需要添加'self.navigationController ?. navigationBar'in' self.navigationBar' –

+0

上面的viewDidLoad()函数位于不在每个视图控制器类中的单独的导航控制器类中。 –

回答

0

添加一个渐变的底部的图像视图完成我所要做的事情吧,感谢@ Anbu.Karthik将我能够适应的另一个问题联系起来。这是我现在的代码:

import UIKit 

class navigationController:UINavigationController, UIViewControllerTransitioningDelegate{ 

override func viewDidLoad() 
    { 
     var appblue = UIColor(red: 109/255, green: 208/255, blue:247/255, alpha: 1.0) 
     self.navigationBar.barTintColor = appblue 
     self.navigationBar.layer.masksToBounds = false 

     var yPosition = self.navigationBar.layer.frame.height 
     var rectWidth = self.navigationBar.layer.frame.width 
     let gradient = CAGradientLayer() 
     let bottomShadow: UIImageView = UIImageView() 
     bottomShadow.frame = CGRect(x: 0, y: yPosition, width: rectWidth, height: 8) 
     gradient.frame = bottomShadow.bounds 
     gradient.colors = [UIColor.lightGray.cgColor, UIColor(white: 1, alpha: 0).cgColor] 

     navigationBar.addSubview(bottomShadow) 
     bottomShadow.layer.insertSublayer(gradient, at: 0) 
    } 
}