@@ -23,30 +23,40 @@ extension Extendable where Base: UIView {
23
23
///
24
24
/// - Parameters:
25
25
/// - direction: Gradient direction.
26
+ /// - forceLayout: Whether to forcefully call `layoutIfNeeded`.
27
+ /// Set to `true` in cases where cells are reused; otherwise,
28
+ /// it can be determined based on the situation, typically `false`.
26
29
/// - colors: The colors for the gradient.
27
30
/// - Returns: The created gradient layer.
28
- public func createGradientLayer( direction: Direction , colors: [ UIColor ] ) -> CAGradientLayer {
29
- createGradientLayer ( by: . init( direction: direction, colors: . init( colors) ) )
31
+ public func createGradientLayer( direction: Direction , forceLayout : Bool = false , colors: [ UIColor ] ) -> CAGradientLayer {
32
+ createGradientLayer ( by: . init( direction: direction, colors: . init( colors) ) , forceLayout : forceLayout )
30
33
}
31
34
32
35
/// Conveniently applies a gradient to the view using preset positions from the `Direction` enumeration.
33
36
///
34
37
/// - Parameters:
35
38
/// - direction: Gradient direction.
39
+ /// - forceLayout: Whether to forcefully call `layoutIfNeeded`.
40
+ /// Set to `true` in cases where cells are reused; otherwise,
41
+ /// it can be determined based on the situation, typically `false`.
36
42
/// - colors: The colors for the gradient.
37
43
/// - Returns: The created gradient layer.
38
44
@discardableResult
39
- public func setGradient( direction: Direction , colors: [ UIColor ] ) -> CAGradientLayer {
40
- applyGradient ( with: . init( direction: direction, colors: . init( colors) ) )
45
+ public func setGradient( direction: Direction , forceLayout : Bool = false , colors: [ UIColor ] ) -> CAGradientLayer {
46
+ applyGradient ( with: . init( direction: direction, colors: . init( colors) ) , forceLayout : forceLayout )
41
47
}
42
48
}
43
49
44
50
extension Extendable where Base: UIView {
45
- /// Creates a gradient layer for the view.
51
+ /// Creates a gradient layer for the view
46
52
///
47
- /// - Parameter gradient: The gradient configuration.
48
- /// - Returns: The created gradient layer.
49
- public func createGradientLayer( by gradient: Gradient ) -> CAGradientLayer {
53
+ /// - Parameters:
54
+ /// - gradient: Gradient configuration
55
+ /// - forceLayout: Whether to forcefully call `layoutIfNeeded`.
56
+ /// Set to `true` in cases where cells are reused; otherwise,
57
+ /// it can be determined based on the situation, typically `false`.
58
+ /// - Returns: The created gradient layer
59
+ public func createGradientLayer( by gradient: Gradient , forceLayout: Bool = false ) -> CAGradientLayer {
50
60
let start = gradient. startPosition
51
61
let end = gradient. endPosition
52
62
@@ -62,7 +72,7 @@ extension Extendable where Base: UIView {
62
72
return layer
63
73
}
64
74
65
- if base. layer. bounds. isEmpty {
75
+ if forceLayout || base. layer. bounds. isEmpty {
66
76
base. layoutIfNeeded ( )
67
77
base. superview? . layoutIfNeeded ( )
68
78
}
@@ -80,13 +90,17 @@ extension Extendable where Base: UIView {
80
90
81
91
/// Applies the gradient configuration to the view.
82
92
///
83
- /// - Parameter gradient: The gradient configuration.
93
+ /// - Parameters:
94
+ /// - gradient: Gradient configuration
95
+ /// - forceLayout: Whether to forcefully call `layoutIfNeeded`.
96
+ /// Set to `true` in cases where cells are reused; otherwise,
97
+ /// it can be determined based on the situation, typically `false`.
84
98
/// - Returns: The created gradient layer.
85
99
@discardableResult
86
- public func applyGradient( with gradient: Gradient ) -> CAGradientLayer {
100
+ public func applyGradient( with gradient: Gradient , forceLayout : Bool = false ) -> CAGradientLayer {
87
101
removeGradientLayer ( )
88
102
89
- let gradientLayer = createGradientLayer ( by: gradient)
103
+ let gradientLayer = createGradientLayer ( by: gradient, forceLayout : forceLayout )
90
104
91
105
base. layer. insertSublayer ( gradientLayer, at: 0 )
92
106
base. backgroundColor = . clear
0 commit comments