Post

Replies

Boosts

Views

Activity

Accessing an environmentvariable from a SwiftUI func
I've got a SwiftUI app that uses two separate web views to display html pages stored in a CoreData database. When the user clicks on a link in one web view, the linked file is shown in the second web view. To make this happen I'm using the following: struct LibCompWebView: UIViewRepresentable {     let request: URLRequest     func makeCoordinator() -> Coordinator {         .init(self)     }     func makeUIView(context:Context) -> WKWebView {         let webView = WKWebView()         webView.navigationDelegate = context.coordinator         return webView     }     func updateUIView(_ webView: WKWebView, context: Context) {         webView.load(request)     }     class Coordinator: NSObject, WKNavigationDelegate {         var webView: LibCompWebView         init(_ webView:LibCompWebView) {             self.webView = webView         }         func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {             if navigationAction.navigationType == .linkActivated { navigateLibCompTo(theURL:navigationAction.request.url!)                 decisionHandler(.cancel)             } else {                 print("not a user click")                 decisionHandler(.allow)             }         }     } } In that NavigationDelegate is a call to a function called navigateLibCompTo, which passes the URL that the user clicked on to a function that is then supposed to look up the appropriate stuff from my coredata entity and load it into the appropriate web view. I'm sharing my CoreData stuff as a StateObject accessed through an @environmentobject. The problem is that my navigateLibComp function is outside of my view hierarchy, so I have no way to access the CoreData stuff. I'm completely stumped!
0
0
408
Oct ’21
ListStyle weirdness in iOS 15
iOS 15 has messed up the appearance of some of my lists. Try this code: import SwiftUI struct ContentView: View { //    let arrayOfStuff = ["Horse","Cow","Screwdriver","Blender","Pyramid","Scotch Tape","Baseball card","CD","Checkbook","Fountain pen","Pin cushion","Couch","Potatos","Protractor"] let arrayOfStuff = ["Horse","Cow","Screwdriver","Blender"] @State var currentlySelectedItem = ""     var body: some View {         ZStack {             Color(.red)                 .ignoresSafeArea()             List {                 ForEach(arrayOfStuff, id: \.self){ item in                     Text(item)                         .onTapGesture {                             currentlySelectedItem = item                         }                         .listRowBackground(currentlySelectedItem == item ? Color.gray : Color(UIColor.systemGroupedBackground))                 }             } //            .listStyle(GroupedListStyle())             .listStyle(PlainListStyle())             .frame(width: 500, height: 500, alignment: .leading)         }     } } When running with the plain list style, the area of the frame that doesn't include any rows becomes transparent. When running with the GroupedListStyle, the empty area stays white. That's the behavior I want, but I want the tighter margins of the plain list style. It was all working fine in iOS 14.
0
0
731
Sep ’21
Confusion over applicationActivities in a UIViewController
I'm using this code to create a Share Sheet: https://swiftui.gallery/uploads/code/ShareSheet.html and I've got no problem bringing up a Share Sheet over another sheet. However, I'm having trouble getting it configured to share what I want. I'm trying to share a zip file out of my app. I've got an NSURL to the file I want to share, which I can pass with no problem, but it fails when I try to airdrop it. I assume this has something to do with applicationActivities?
0
0
411
Sep ’21
Trouble with a share sheet on a sheet in iOS 15
I've got a Preferences system that I've implemented as a sheet. Within that I have an export command that bundles up some data and then offers a share sheet. Under iOS 14, I could trigger this share sheet from the onDismiss handler in the Preferences sheet call. Under iOS 15, it appears that onDismiss executes before the Preferences sheet goes away, so my Share Sheet never appears. Here's the code that activates the Preferences sheet, which includes the onDismiss handler: .sheet(isPresented: $theCurrentLog.prefsAreVisible, onDismiss: { if currentPrefs.thePreferences.exportToShare {           let av = UIActivityViewController(activityItems: [currentPrefs.thePreferences.exportURL], applicationActivities: nil) UIApplication.shared.windows.first?.rootViewController?.present(av, animated: true, completion: nil)       if UIDevice.current.userInterfaceIdiom == .pad {       av.popoverPresentationController?.sourceView = UIApplication.shared.windows.first                     av.popoverPresentationController?.sourceRect = CGRect (                         x: UIScreen.main.bounds.width / 2.1,                         y: UIScreen.main.bounds.height / 2.3,                         width: 200, height: 200)                 }                 currentPrefs.thePreferences.exportToShare = false             }             let prefsToSave:APreferenceSet = APreferenceSet()             prefsToSave.thePreferences = currentPrefs.thePreferences             prefsToSave.savePrefs()         }         ) { Prefs()} That exportToShare boolean in the if statement is set inside the the Preferences sheet. Again, this all worked under iOS 14. Any ideas how to fix it for 15? Thanks
1
0
522
Sep ’21
In SwiftUI, how to rotate a background image when orientation changes
I've got some views that have background images, and I would like to switch those images to different images, when the orientation changes. I'm following this example to track when the orientation changes: https://www.hackingwithswift.com/quick-start/swiftui/how-to-detect-device-rotation So this gives me a way to track when orientation changes, but I can't find anywhere to use that information because the Views will already have drawn by the time I can execute any code.
1
0
1.4k
Sep ’21
Accessing an environmentvariable from a SwiftUI func
I've got a SwiftUI app that uses two separate web views to display html pages stored in a CoreData database. When the user clicks on a link in one web view, the linked file is shown in the second web view. To make this happen I'm using the following: struct LibCompWebView: UIViewRepresentable {     let request: URLRequest     func makeCoordinator() -> Coordinator {         .init(self)     }     func makeUIView(context:Context) -> WKWebView {         let webView = WKWebView()         webView.navigationDelegate = context.coordinator         return webView     }     func updateUIView(_ webView: WKWebView, context: Context) {         webView.load(request)     }     class Coordinator: NSObject, WKNavigationDelegate {         var webView: LibCompWebView         init(_ webView:LibCompWebView) {             self.webView = webView         }         func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {             if navigationAction.navigationType == .linkActivated { navigateLibCompTo(theURL:navigationAction.request.url!)                 decisionHandler(.cancel)             } else {                 print("not a user click")                 decisionHandler(.allow)             }         }     } } In that NavigationDelegate is a call to a function called navigateLibCompTo, which passes the URL that the user clicked on to a function that is then supposed to look up the appropriate stuff from my coredata entity and load it into the appropriate web view. I'm sharing my CoreData stuff as a StateObject accessed through an @environmentobject. The problem is that my navigateLibComp function is outside of my view hierarchy, so I have no way to access the CoreData stuff. I'm completely stumped!
Replies
0
Boosts
0
Views
408
Activity
Oct ’21
ListStyle weirdness in iOS 15
iOS 15 has messed up the appearance of some of my lists. Try this code: import SwiftUI struct ContentView: View { //    let arrayOfStuff = ["Horse","Cow","Screwdriver","Blender","Pyramid","Scotch Tape","Baseball card","CD","Checkbook","Fountain pen","Pin cushion","Couch","Potatos","Protractor"] let arrayOfStuff = ["Horse","Cow","Screwdriver","Blender"] @State var currentlySelectedItem = ""     var body: some View {         ZStack {             Color(.red)                 .ignoresSafeArea()             List {                 ForEach(arrayOfStuff, id: \.self){ item in                     Text(item)                         .onTapGesture {                             currentlySelectedItem = item                         }                         .listRowBackground(currentlySelectedItem == item ? Color.gray : Color(UIColor.systemGroupedBackground))                 }             } //            .listStyle(GroupedListStyle())             .listStyle(PlainListStyle())             .frame(width: 500, height: 500, alignment: .leading)         }     } } When running with the plain list style, the area of the frame that doesn't include any rows becomes transparent. When running with the GroupedListStyle, the empty area stays white. That's the behavior I want, but I want the tighter margins of the plain list style. It was all working fine in iOS 14.
Replies
0
Boosts
0
Views
731
Activity
Sep ’21
Confusion over applicationActivities in a UIViewController
I'm using this code to create a Share Sheet: https://swiftui.gallery/uploads/code/ShareSheet.html and I've got no problem bringing up a Share Sheet over another sheet. However, I'm having trouble getting it configured to share what I want. I'm trying to share a zip file out of my app. I've got an NSURL to the file I want to share, which I can pass with no problem, but it fails when I try to airdrop it. I assume this has something to do with applicationActivities?
Replies
0
Boosts
0
Views
411
Activity
Sep ’21
Trouble with a share sheet on a sheet in iOS 15
I've got a Preferences system that I've implemented as a sheet. Within that I have an export command that bundles up some data and then offers a share sheet. Under iOS 14, I could trigger this share sheet from the onDismiss handler in the Preferences sheet call. Under iOS 15, it appears that onDismiss executes before the Preferences sheet goes away, so my Share Sheet never appears. Here's the code that activates the Preferences sheet, which includes the onDismiss handler: .sheet(isPresented: $theCurrentLog.prefsAreVisible, onDismiss: { if currentPrefs.thePreferences.exportToShare {           let av = UIActivityViewController(activityItems: [currentPrefs.thePreferences.exportURL], applicationActivities: nil) UIApplication.shared.windows.first?.rootViewController?.present(av, animated: true, completion: nil)       if UIDevice.current.userInterfaceIdiom == .pad {       av.popoverPresentationController?.sourceView = UIApplication.shared.windows.first                     av.popoverPresentationController?.sourceRect = CGRect (                         x: UIScreen.main.bounds.width / 2.1,                         y: UIScreen.main.bounds.height / 2.3,                         width: 200, height: 200)                 }                 currentPrefs.thePreferences.exportToShare = false             }             let prefsToSave:APreferenceSet = APreferenceSet()             prefsToSave.thePreferences = currentPrefs.thePreferences             prefsToSave.savePrefs()         }         ) { Prefs()} That exportToShare boolean in the if statement is set inside the the Preferences sheet. Again, this all worked under iOS 14. Any ideas how to fix it for 15? Thanks
Replies
1
Boosts
0
Views
522
Activity
Sep ’21
In SwiftUI, how to rotate a background image when orientation changes
I've got some views that have background images, and I would like to switch those images to different images, when the orientation changes. I'm following this example to track when the orientation changes: https://www.hackingwithswift.com/quick-start/swiftui/how-to-detect-device-rotation So this gives me a way to track when orientation changes, but I can't find anywhere to use that information because the Views will already have drawn by the time I can execute any code.
Replies
1
Boosts
0
Views
1.4k
Activity
Sep ’21