文章目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

let gradientLayer:CAGradientLayer = CAGradientLayer()
gradientLayer.frame = CGRect.init(x: 0, y: 0, width: rect.size.width, height: 64)
let color1 = colorWithHexString(hex: "#4238f0")
let color2 = colorWithHexString(hex: "#108bfc")
let color3 = colorWithHexString(hex: "#00a3ff")



gradientLayer.colors = [color3.cgColor, color2.cgColor,color1.cgColor]
//设置渐变范围
gradientLayer.locations = [0.0 ,0.5,1.0];
gradientLayer.startPoint=CGPoint.init(x: 0, y: 0.5)
gradientLayer.endPoint = CGPoint.init(x: 1, y: 0.5)

gradientLayer.frame = CGRect.init(x: 0, y: 0, width: rect.size.width, height: kTopHeight)


if #available(iOS 11.0, *) {

for view in self.navigationBar.subviews {
if view .isKind(of: NSClassFromString("_UIBarBackground")!){
for v in view.subviews{
if v.isKind(of: NSClassFromString("UIVisualEffectView")!){
for e in v.subviews {
if e.isKind(of: NSClassFromString("_UIVisualEffectSubview")!){
e.layer.addSublayer(gradientLayer)
}
}
}
}
}
}
}else if #available(iOS 10.0, *){

let view = self.navigationBar.subviews.first
for v in (view?.subviews)!{
if v.isKind(of: NSClassFromString("UIVisualEffectView")!){
for e in v.subviews {
if e.isKind(of: NSClassFromString("_UIVisualEffectFilterView")!){
e.layer.addSublayer(gradientLayer)
}
}
}
}

}else if #available(iOS 9.0, *){

let view = self.navigationBar.subviews.first
view?.layer.addSublayer(gradientLayer)
}else if #available(iOS 8.0, *){

let view = self.navigationBar.subviews.first
view?.layer.addSublayer(gradientLayer)
}
文章目录