SwiftUIKit provides a collection of common controls for use with SwiftUI.
If you find SwiftUIKit useful and would like to help support its continued development and maintenance, please consider making a small donation, especially if you are using it in a commercial product:
It's through the support of contributors like yourself, I can continue to build, release and maintain high-quality, well documented Swift Packages like SwiftUIKit for free.
Swift Package Manager (Xcode 11 and above)
- In Xcode, select the File > Add Package Dependency… menu item.
- Paste
https://github.com/Appracatappra/SwiftUIKit.gitin the dialog box. - Follow the Xcode's instruction to complete the installation.
Why not CocoaPods, or Carthage, or etc?
Supporting multiple dependency managers makes maintaining a library exponentially more complicated and time consuming.
Since, the Swift Package Manager is integrated with Xcode 11 (and greater), it's the easiest choice to support going further.
SwiftUIKit provides a simply way to display alerts in your SwiftUI Views and several useful new controls including:
- CircleText - The
CircleTextcontrol will display text around a circle in aSwiftUI View. - ContentButton - A
ContentButtonis a special type of SwiftUIbuttonthat works and lays out the same way on tvOS devices as it does on mobile devices. - IconButton -
IconButtonis aSwiftUIcontrol that is displayed as a rounded edge Button with a border and icon.IconButtonwill work with both touch based and focus base UIs. - IconDescriptionButton -
IconDescriptionButtonis aSwiftUIcontrol that is displayed as a rounded edge Button with a border and icon along with a description block of text. TheIconDescriptionButtonmake great user preference controls and will work with both touch based and focus base UIs. - OnOffToggleButton -
OnOffToggleButtonis aSwiftUIcontrol that is displayed as a rounded edge Button with a border and icon.OnOffToggleButtonwill flip between the on and off states when clicked and works with both touch based and focus base UIs. - ScaledImageButton -
ScaledImageButtonis aSwiftUIcontrol that displays the given image as a button.ScaledImageButtonwill work with both touch based and focus base UIs. - ScaledImageView -
ScaledImageViewis aSwiftUIcontrol that displays an image in aSwiftUI Viewscaled to a give ratio. The image is scaled directly from the disk storage so that it takes less actual memory in the device. - SelectIntOptionsButton -
SelectIntOptionsButtonis aSwiftUIcontrol that is displayed as a rounded edge Button with a border, icon and descriptive text block. When clicked, theSelectIntOptionsButtonwill cycle through the list of options provided.SelectIntOptionsButtonwill work with both touch based and focus base UIs and makes a great user preference control. - WordArtButton -
WordArtButtonis aSwiftUIcontrol that is displayed as interactable Word Art.WordArtButtonwill work with both touch based and focus base UIs. - WordArtView -
WordArtViewdisplays text in the given font at the given size and rotation with the defined gradient. - ZoomView -
ZoomViewA zoomable, scrollable container for the given SwiftUI content. It provides buttons to zoom in & out and to return to the default zoom level. - ScaleableWaitingIndicator - A View that displays an animated waiting indicator that can be scaled to any size desired.
There are two default sounds embedded in SwiftUIKit:
- diamond-click.mp3 - Used when a control gets focus.
- mouse-click.mp3 - Used when the control is clicked.
Both sounds were sourced from Freeound.org under the Creative Commons 0 License.
The SwiftUIKit provides a few helper utilities that allow you to easily access resources stored in the Swift Package (such as the sounds above).
For example, the following code would return the path to the diamond-click.mp3 file:
let path = SwiftUIKit.pathTo(resource:"diamond-click.mp3")
Several of the controls defined in SwiftUIKit have a static set of properties to control all instances of the control created without specifying those properties.
For example, the IconButton defines:
/// The default background color for the `IconButton`.
static public var defaultBackgroundColor:Color = .gray
/// The default sound source for the `IconButton`.
static public var defaultSoundSource:SwiftUIKit.Source = .packageBundle
/// The default button clicked sound `IconButton`.
static public var defaultButtonSound:String = "mouse-click.mp3"
/// The default focused sound for the `IconButton`.
static public var defaultButtonFocusSound:String = "diamond-click.mp3"
If you wanted to universally style all of the IconButton instances used throughout you App, simply adjust the defaultBackgroundColor of IconButton:
IconButton.defaultBackgroundColor = .blue
Now all new IconButtons created will have a Blue background.
For style changes to be in effect, you'll need to make the changes before any Views are drawn. You can use the following code on your main app:
import SwiftUI
import SwiftletUtilities
import LogManager
import SwiftUIKit
@main
struct PackageTesterApp: App {
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
@Environment(\.scenePhase) private var scenePhase
@Environment(\.colorScheme) var colorScheme
var body: some Scene {
WindowGroup {
ContentView()
}
.onChange(of: scenePhase) { oldScenePhase, newScenePhase in
switch newScenePhase {
case .active:
Debug.info(subsystem: "PackageTesterApp", category: "Scene Phase", "App is active")
case .inactive:
Debug.info(subsystem: "PackageTesterApp", category: "Scene Phase", "App is inactive")
case .background:
Debug.info(subsystem: "PackageTesterApp", category: "Scene Phase", "App is in background")
@unknown default:
Debug.notice(subsystem: "PackageTesterApp", category: "Scene Phase", "App has entered an unexpected scene: \(oldScenePhase), \(newScenePhase)")
}
}
}
}
/// Class the handle the event that would typically be handled by the Application Delegate so they can be handled in SwiftUI.
class AppDelegate: NSObject, UIApplicationDelegate {
/// Handles the app finishing launching
/// - Parameter application: The app that has started.
func applicationDidFinishLaunching(_ application: UIApplication) {
// Register to receive remote notifications
UIApplication.shared.registerForRemoteNotifications()
}
/// Handle the application getting ready to launch
/// - Parameters:
/// - application: The application that is going to launch.
/// - launchOptions: Any options being passed to the application at launch time.
/// - Returns: Returns `True` if the application can launch.
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// Set any `SwiftUIKit` global style defaults here before any `Views` are drawn.
// Set style defaults
IconButton.defaultBackgroundColor = .blue
return true
}
/// Handles the app receiving a remote notification
/// - Parameters:
/// - application: The app receiving the notifications.
/// - userInfo: The info that has been sent to the App.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
}
}
With this code in place, make any style changes in func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool and they apply to all views built afterwards.
The Package includes full DocC Documentation for all of its features.
