func setGradientBackgroundColors() {
let gradient = CAGradientLayer()
gradient.startPoint = CGPoint(x:0,y:0.8)
gradient.endPoint = CGPoint(x:1,y:0.1)
gradient.locations = [0,1]
gradient.frame = CGRect(x:0,y:0,width:kScreenW,height:self.navigationBar.bounds.height+kStatusBarH)
gradient.colors = naviBackgroundCGColors
self.navigationBar.layer.addSublayer(gradient)
}
//經(jīng)過一段時(shí)間使用發(fā)現(xiàn)啡专,這種寫法有問題,當(dāng)你在NavigationController的某一個(gè)childViewController設(shè)置self.navigationController.navigationBar.hidden = YES; 即某一個(gè)VC中需要隱藏導(dǎo)航欄(我這邊是rootViewController需要隱藏)制圈,在其他子控制器(未隱藏)設(shè)置 self.navigationItem 的 title 或titleView 會(huì)出現(xiàn)不顯示的問題
解決方案:
通過查看圖層可以獲悉設(shè)置背景色的視圖類名為 _UIBarBackground 因此们童,可以不必在navBar中插入子圖層 (subLayer) (這也是出現(xiàn)這個(gè)bug的原因所在) ,將漸變圖層加入到 _UIBarBackground 中鲸鹦,代碼如下:
for sub in self.navigationBar.subviews{
if sub.isKind(of:(NSClassFromString("_UIBarBackground").self)!){
sub.layer.addSublayer(gradient)
}