Tips #7
Replies: 12 comments 10 replies
-
I'll start. Capture the previous value in
|
Beta Was this translation helpful? Give feedback.
-
What’s the difference between a background view and a
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
macOS preferences window@main
struct AppMain: App {
var body: some Scene {
Settings {
TabView {
Text("General Pane")
.tabItem {
Label("General", systemImage: "gearshape")
}
Text("Advanced Pane")
.tabItem {
Label("Advanced", systemImage: "gearshape.2")
}
}
.frame(width: 400, height: 200)
}
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Multiple sheet/popover/alert's attached to the same elementYou cannot currently have multiple sheets, for example, attached to the same element (FB7367599). We can work around this with some extensions: extension View {
/// This allows multiple sheets on a single view, which `.sheet()` doesn't.
func sheet2<Content: View>(
isPresented: Binding<Bool>,
onDismiss: (() -> Void)? = nil,
@ViewBuilder content: @escaping () -> Content
) -> some View {
background(
EmptyView().sheet(
isPresented: isPresented,
onDismiss: onDismiss,
content: content
)
)
}
/// This allows multiple alerts on a single view, which `.alert()` doesn't.
func alert2(
isPresented: Binding<Bool>,
content: @escaping () -> Alert
) -> some View {
background(
EmptyView().alert(
isPresented: isPresented,
content: content
)
)
}
/// This allows multiple popovers on a single view, which `.popover()` doesn't.
func popover2<Content: View>(
isPresented: Binding<Bool>,
attachmentAnchor: PopoverAttachmentAnchor = .rect(.bounds),
arrowEdge: Edge = .top,
@ViewBuilder content: @escaping () -> Content
) -> some View {
background(
EmptyView().popover(
isPresented: isPresented,
attachmentAnchor: attachmentAnchor,
arrowEdge: arrowEdge,
content: content
)
)
}
} |
Beta Was this translation helpful? Give feedback.
-
Only show loading indicator after 1 secondThis can be useful to prevent flashing when content loads quickly. if isLoading {
LoadingView()
.transition(.opacity)
.animation(
Animation
.easeIn(duration: 0.5)
.delay(1)
)
} |
Beta Was this translation helpful? Give feedback.
-
Detect taps on
|
Beta Was this translation helpful? Give feedback.
-
Show a fullscreen gradient background, but still position in the safe areaThe Applying the ContentView()
.background(
LinearGradient(gradient: theGradient, startPoint: .top, endPoint: .bottom)
.ignoresSafeArea()
) Example: app screenshot. |
Beta Was this translation helpful? Give feedback.
-
Pass a
|
Beta Was this translation helpful? Give feedback.
-
Initialize dependent
|
Beta Was this translation helpful? Give feedback.
-
Platform specific modifiersNot all API is available on all platforms, for example MyView()
#if !os(macOS)
.navigationBarTitle("xyz")
#endif @sindresorhus provided a nice example on how to approach this on StackOverflow. I modified that a little: extension View {
/**
* Run a modifying closure on the View. This can be used for per OS
* switches, like:
*
* MyView()
* .modify {
* #if os(macOS)
* $0
* #else
* $0.navigationBarTitle("XYZ")
* #endif
* }
*/
@inlinable
func modify<T: View>(@ViewBuilder modifier: ( Self ) -> T) -> T {
return modifier(self)
}
} Don't forget to return Forum discussion: https://forums.swift.org/t/swiftui-extension-for-os-specific-view-modifiers-that-seems-too-arcane-to-implement/30897/11 |
Beta Was this translation helpful? Give feedback.
-
Share some SwiftUI quick tips.
Addition guidelines:
### Title
for the tip title.Beta Was this translation helpful? Give feedback.
All reactions