Post

Replies

Boosts

Views

Activity

Reply to How to achieve a pure backdrop blur effect without predefined tint color in SwiftUI / UIKit?
You can remove unnecessary filters (luminanceCurveMap, colorSaturate, colorBrightness) from UIVisualEffectView and leave only gaussianBlur. Example: https://github.com/VAndrJ/VAFrostedGlassView/blob/main/VAFrostedGlassView/Classes/VAThicknessVisualEffectView.swift CAFilter is not public, but here is some information: https://theapplewiki.com/wiki/Dev:CAFilter Or use a third-party, GPUImage3 for example: https://github.com/BradLarson/GPUImage3
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to Is there a global Alert View in SwiftUI?
You can add an alert to the highest view in the current view tree hierarchy and pass it on, for example, via Environment. Briefly, one of the options: extension EnvironmentValues { @Entry var alertError: Binding<Error?> = .constant(nil) } enum Destination { case screen2 case screen3 } struct ContentView: View { @State private var presentedError: Error? = nil var body: some View { NavigationStack { Screen1() .navigationDestination(for: Destination.self) { destination in switch destination { case .screen2: Screen2() case .screen3: Screen3() } } } .environment(\.alertError, $presentedError) .alert( "Error!", isPresented: .constant(presentedError != nil), presenting: presentedError, actions: { _ in Button("Everything is lost :(") { presentedError = nil } }, message: { error in Text(error.localizedDescription) } ) } } struct Screen1: View { @Environment(\.alertError) private var presentedError var body: some View { VStack { Text("Screen1") NavigationLink("Next", value: Destination.screen2) .padding() Button("Trigger Error") { presentedError.wrappedValue = MyAwesomeError.screen1 } } } } struct Screen2: View { @Environment(\.alertError) private var presentedError var body: some View { VStack { Text("Screen2") NavigationLink("Next", value: Destination.screen3) Button("Trigger Error") { presentedError.wrappedValue = MyAwesomeError.screen2 } } } } struct Screen3: View { @Environment(\.alertError) private var presentedError var body: some View { VStack { Text("Screen3") Button("Trigger Error") { presentedError.wrappedValue = MyAwesomeError.screen3 } } } } enum MyAwesomeError: Error, LocalizedError { case screen1 case screen2 case screen3 var errorDescription: String? { String(describing: self) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’25
Reply to How to await inside SwiftUI a #Preview?
@Developer Tools Engineer it will cause a crash even when Book is in the SwiftUI environment. Because the @Environment value got called multiple times, and some of that calls with a default value. Here I tried your example in a simplified version and got a crash: Then I added a default Book and it started to work: This became especially noticeable when, in Xcode 16.2, the @Entry macro was changed, and it started generating code with a computed property for defaultValue instead of a stored property. Used code: import SwiftUI struct Book { // Interesting properties here... let title: String static func createBook(title: String) async -> Book { // Await the content here... // Initialize the Book with actual content... return Book(title: title) } } extension EnvironmentValues { @Entry var previewBook: Book = { // let bookTitle = "default book to avoid crashes in preview" // print(bookTitle) // return Book(title: bookTitle) // This is the default value and will only be used if you forget to set your preview modifier // trait. fatalError("No value set for previewBook in the environment, did you forget your preview modifier trait?") }() } extension View { func previewBook(_ book: Book) -> some View { environment(\.previewBook, book) } } struct BookContent: PreviewModifier { static func makeSharedContext() async throws -> Book { // This async throws function is called to create the context for the preview modifer // that can then be applied to preview content. return await Book.createBook(title: "Example") } // This is called to modify your preview content with the context created above whenever // you apply your preview modifier to a #Preview. func body(content: Content, context: Book) -> some View { content.previewBook(context) } } extension PreviewTrait where T == Preview.ViewTraits { // Create a convenience for applying the above preview modifier to a preview static var previewBook: Self { .modifier(BookContent()) } } struct BookView: View { let book: Book var body: some View { VStack { Text(book.title) .padding() } } } #Preview(traits: .previewBook) { // @Previewable allows us to use normal SwiftUI attributes like @Environment, @State, // and @Binding in a preview body @Previewable @Environment(\.previewBook) var book BookView(book: book) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’25
Reply to How to await inside SwiftUI a #Preview?
@Developer Tools Engineer , you are giving a bad example because this will lead to a crash: extension EnvironmentValues { @Entry var previewBook: Book = { // This is the default value and will only be used if you forget to set your preview modifier // trait. fatalError("No value set for previewBook in the environment, did you forget your preview modifier trait?") }() }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’25
Reply to Does LazyVStack and LazyVGrid release views from memory inside a ScrollView?
@DTS Engineer , but the question was: Does LazyVStack and LazyVGrid release views from memory inside a ScrollView? in iOS 18 And not about loading views incrementally. With such a big change, why not make it a separate component? Or at least add a parameter to existing? For example, so it looks like this: ReusableVStack { ... } // or LazyVStack(reuseViews: true) { ... }
Topic: UI Frameworks SubTopic: SwiftUI
Feb ’25
Reply to What does the @__EntryDefaultValue macro expand to?
Well, if Xcode doesn't show it, there are other ways. And indeed, this @__EntryDefaultValue macro makes the variable computed. So in Xcode 16.2, the behavior of @Entry is different from Xcode 16.1. Considering the number of bugs in SwiftUI, changing the existing behavior and adding such ambiguity is not a good solution.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’25