A Swift package for common SwiftUI components and helper methods.
- iOS 13, macOS 10.15, tvOS 13, or watchOS 6
- Swift 5.5+
- Xcode 13.0+
Install SwiftySwiftUI via Swift Package Manager.
- In Xcode, open your project and navigate to File → Swift Packages → Add Package Dependency...
- Paste the repository URL
https://github.com/lmartinresnick/SwiftySwiftUIand click Next. - For Rules, select Branch, with branch set to
main. - Click Finish.
.embedInNavigationView()- Instantly embed your
ViewinNavigationView
VStack {
List {
NavigationLink("Purple", destination: ColorDetail(color: .purple))
NavigationLink("Pink", destination: ColorDetail(color: .pink))
NavigationLink("Orange", destination: ColorDetail(color: .orange))
}
.navigationTitle("Colors")
}
.embedInNavigationView().eraseToAnyView()- Instantly "erase" your
ViewtoAnyView
VStack {
Text("Erase to AnyView")
}
.eraseToAnyView().frame(_:)- Set
framewith the samewidthandheight
Circle()
.frame(50)extension View {
func frame(_ dimensions) -> some View {
self
.frame(width: dimensions, height: dimensions)
}
}.if(_:transform:)- Use an
ifcondition to show/hide a view modifier
@State private var usernameIsAvailable: Bool = false
var body: some View {
Button("Check username") {
// API call to check if username is available
}
.padding()
.if(usernameIsAvailable) { view in
view.background(Color.green)
}
}Hidden View Modifier
.isHidden(_:remove:)- Use a
booleanto show/hide aView
Text("Error message!")
.isHidden(true)
Text("Error message!")
.isHidden(false).loadingStyle(state:loadingContent:)- Use a
@Stateproperty to display a loading view
@State private var isFetchingAPI: Bool = true
var body: some View {
AsyncImage(url: viewModel.url)
.loadingStyle(state: isFetchingAPI) {
CustomLoadingView()
}
}.bottomSheet(isPresented:showBottomSheet:content:)- Customizable modal presented from the bottom of the screen
- Interacts the same way as
.sheet, i.e. use a@Stateboolean to showBottomSheet
@State private var showBottomSheet: Bool = false
var body: some View {
VStack {
Text("Let's show a bottom sheet")
Button("Tap me to show a bottom sheet") {
showBottomSheet.toggle()
}
}
.bottomSheet(isPresented: showBottomSheet, height: .half) {
CustomBottomSheetView()
}
}