Trying to add a full screen activity indicator in SwiftUI.
I can use .overlay(overlay: )
function in View
Protocol.
With this, I can make any view overlay, but I can't find the iOS default style UIActivityIndicatorView
equivalent in SwiftUI
.
How can I make a default style spinner with SwiftUI
?
NOTE: This is not about adding activity indicator in UIKit framework.
If you want to turn on and off network indicator you need to pass Binding
Example: SwiftUI struct where network indicator will present once customers click on the Button.
NetworkIndicatorSwiftView from Matteo so we can create network indicator from UIKit
When I want to turn on/off I am using Binding. To pass Binding from SwiftUI struct Binding<> and to change value I am using networkIndicator.wrappedValue = true
If you want to a swift-ui-style solution, then this is the magic:
Simply to use:
Hope it helps!
Activity indicator in SwiftUI
Quite a few views are not yet represented in
SwiftUI
, but it's easily to port them into the system. You need to wrapUIActivityIndicator
and make itUIViewRepresentable
.(More about this can be found in the excellent WWDC 2019 talk - Integrating SwiftUI)
Then you can use it as follows - here's an example of a loading overlay.
Note: I prefer using
ZStack
, rather thanoverlay(:_)
, so I know exactly what's going on in my implementation.To test it, you can use this example code:
Result:
Tested on Xcode 11.1
Fully customizable Standard
UIActivityIndicator
in SwiftUI: (Exactly as a nativeView
):Base Struct:
Extension:
With this little helpful extension, you can access the configuration through a
modifier
like other SwiftUIview
s:Customization:
You can configure it as much as you could in the original
UIKit
:Exactly like SwiftUI:
The classic way:
Also you can configure the view in a classic initializer:
Result:
This method is fully adaptable. For example you can see How to make TextField become first responder with the same method here