For snapshot testing purposes I need to disable the animation of a SwiftUI ProgressView
with CircularProgressViewStyle
...
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
Does anyone know how to do this please? I have already tried the following which do not work...
Doesn't Work
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
.disabled(true)
Doesn't Work
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
.animation(nill)
Doesn't Work
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
.transaction { transaction in
transaction.animation = nil
}
Doesn't Work
ProgressView(value: 0, total: 100)
.progressViewStyle(CircularProgressViewStyle())
.transaction { transaction in
transaction.animation = nil
}
For snapshot testing purposes I need to disable the animation of a SwiftUI ProgressView
with CircularProgressViewStyle
...
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
Does anyone know how to do this please? I have already tried the following which do not work...
Doesn't Work
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
.disabled(true)
Doesn't Work
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
.animation(nill)
Doesn't Work
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
.transaction { transaction in
transaction.animation = nil
}
Doesn't Work
ProgressView(value: 0, total: 100)
.progressViewStyle(CircularProgressViewStyle())
.transaction { transaction in
transaction.animation = nil
}
Share
asked Nov 28, 2024 at 10:41
Oliver PearmainOliver Pearmain
20.6k13 gold badges93 silver badges94 bronze badges
1
|
1 Answer
Reset to default 0Given that this is for testing, it would be convenient and desirable to disable all the progress views globally by swizzling UIActivityIndicatorView.startAnimating
. You would just do the swizzling in the setUp
method of your tests.
extension UIActivityIndicatorView {
@objc func swizzledStartAnimating() {
hidesWhenStopped = false
}
static nonisolated func disableAnimations() {
let originalSelector = #selector(UIActivityIndicatorView.startAnimating)
let swizzledSelector = #selector(UIActivityIndicatorView.swizzledStartAnimating)
let originalMethod = class_getInstanceMethod(self, originalSelector)!
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)!
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
let disableProgressView: () = UIActivityIndicatorView.disableAnimations()
I am calling UIActivityIndicatorView.disableAnimations()
in the initialiser of a global property. This makes sure that it is called exactly once, even if the property's getter is called multiple times. In the setUp
of your tests, you just need to write
_ = disableProgressView
to call the property getter.
Alternatively, use SwiftUI-Introspect to access the underlying UIActivityIndicatorView
:
ProgressView().progressViewStyle(.circular)
.introspect(.progressView(style: .circular), on: .iOS(.v14, .v15, .v16, .v17, .v18)) {
$0.hidesWhenStopped = false
$0.stopAnimating()
}
You will need to add all the versions of iOS that you want to introspect on.
Of course, all of this is based on the assumption that SwiftUI's circular progress view is implemented by UIActivityIndicatorView
. This is the case all the way from iOS 14 when ProgressView
was first added to SwiftUI. If this becomes false in the future, that means ProgressView
is now implemented natively in SwiftUI, in which case one or more of the attempts shown in the question would work.
Image
to replace/cover theProgressView
. – Sweeper Commented Nov 28, 2024 at 10:46