Post

Replies

Boosts

Views

Activity

Reply to Change Navigation Text Title Color on Different SwiftUI Views
Hi I refactored my NavigationBarView and was able to me more "SwiftUI" like in the implementation. Hope this helps: import SwiftUI extension View { func onWillDisappear(_ perform: @escaping () -> Void) -> some View { modifier(ViewWillDisappearModifier(callback: perform)) } func onWillAppear(_ perform: @escaping () -> Void) -> some View { modifier(ViewWillAppearModifier(callback: perform)) } } struct ViewWillDisappearModifier: ViewModifier { let callback: () -> Void func body(content: Content) -> some View { content.background(UIViewControllerResponder(viewWillDisappear: callback)) } } struct ViewWillAppearModifier: ViewModifier { let callback: () -> Void func body(content: Content) -> some View { content.background(UIViewControllerResponder(viewWillAppear: callback)) } } private struct UIViewControllerResponder: UIViewControllerRepresentable { /// Notifies the view controller that its view is about to be added to a view hierarchy. var viewWillAppear: (() -> Void)? = nil /// Notifies the view controller that its view is about to be removed from a view hierarchy. var viewWillDisappear: (() -> Void)? = nil private let controller = ViewController() func makeUIViewController(context: UIViewControllerRepresentableContext<UIViewControllerResponder>) -> UIViewController { if let viewWillAppear = viewWillAppear { controller.viewWillAppear = viewWillAppear } if let viewWillDisappear = viewWillDisappear { controller.viewWillDisappear = viewWillDisappear } return controller } func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<UIViewControllerResponder>) { } /// An object that manages a view hierarchy for your UIKit app. private class ViewController: UIViewController { var viewWillAppear: () -> Void = { } var viewWillDisappear: () -> Void = { } /// Notifies the view controller that its view is about to be added to a view hierarchy. /// - Parameter animated: If true, the view is being added to the window using an animation. override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) viewWillAppear() } /// /// Notifies the view controller that its view is about to be removed from a view hierarchy. /// - Parameter animated: If true, the view is being added to the window using an animation. override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) viewWillDisappear() } } } And in the View, this is how you interact with the events: struct MyView: View { var body: some View { NavigationStack { Text("Home") .navigationTitle("Some Text") .toolbar { ToolbarItem(placement: .primaryAction) { Button { print("Toolbar button pressed") } label: { Image(systemName: "plus") } } } } .onWillAppear { let appearance = UINavigationBarAppearance() appearance.titleTextAttributes = [.foregroundColor: UIColor.white] appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white] UINavigationBar.appearance().standardAppearance = appearance UINavigationBar.appearance().compactAppearance = appearance } .onWillDisappear { let appearance = UINavigationBarAppearance() appearance.titleTextAttributes = [:] appearance.largeTitleTextAttributes = [:] UINavigationBar.appearance().standardAppearance = appearance UINavigationBar.appearance().compactAppearance = appearance } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’24
Reply to Array of Protocols and Subscript
Hi Claude31 I thought this might have to be the solution and of course it works. I did try making enum ServiceType: Int and doing repairs.allowRepairs[ServiceType.boat] which also worked so long as the dictionary value matched the index of the enum. So another issue will be JSON codable of Dictionary[ServiceType: ServiceDescriptor], all sorts of error here. Wouldn't happen to have some ideas around codable of a Dictionary[Enum: Protocols]?
Topic: App & System Services SubTopic: General Tags:
Apr ’22
Reply to XCTest Ambiguous type name
I managed to get the errors to go away. I "unchecked" DefaultValue+Extension.swift and DefaultValuePropertyWrapper.swift from being part of the test target and in the DefaultValueTest.swift file replaced @testable import MyFramework with import MyFramework. Compiles and the tests run successfully now 😅
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’22
Reply to HStack Alignments
After some more trial and error I finally got the look I was after. HStack padding seemed to cause the issue. Here is the final result. import SwiftUI struct ContentView: View {     var body: some View {         VStack(alignment: .center, spacing: 64) {             Text("About My Data")                 .font(.title)                 .fontWeight(.heavy)                 .multilineTextAlignment(.center)                 .padding()             VStack(alignment: .leading, spacing: 32) {                 HStack {                     Image(systemName: "network")                             .font(.system(size: 36.0))                             .foregroundColor(.blue)                     VStack(alignment: .leading) {                        Text("Network Support")                         .font(.headline)                         .fontWeight(.bold)                     Text("Download your data from anywhere.")                         .font(.body)                         .foregroundColor(.gray)                     }                 }                 HStack {                     Image(systemName: "hand.raised.fill")                         .font(.system(size: 36.0))                         .foregroundColor(.blue)                     VStack(alignment: .leading) {                         Text("Data Privacy")                         .font(.headline)                         .fontWeight(.bold)                     Text("Scanned certificates are never stored on the device.")                         .font(.body)                         .foregroundColor(.gray)                     }                 }             }         }.padding()     } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’21
Reply to HStack Alignments
@robnotyou, thanks for replying. I'm trying to achieve making the HStack full width with the image and the VStack text in both containers leading aligned. Because of the text size difference it's slight off.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’21
Reply to Change Navigation Text Title Color on Different SwiftUI Views
Hi I refactored my NavigationBarView and was able to me more "SwiftUI" like in the implementation. Hope this helps: import SwiftUI extension View { func onWillDisappear(_ perform: @escaping () -> Void) -> some View { modifier(ViewWillDisappearModifier(callback: perform)) } func onWillAppear(_ perform: @escaping () -> Void) -> some View { modifier(ViewWillAppearModifier(callback: perform)) } } struct ViewWillDisappearModifier: ViewModifier { let callback: () -> Void func body(content: Content) -> some View { content.background(UIViewControllerResponder(viewWillDisappear: callback)) } } struct ViewWillAppearModifier: ViewModifier { let callback: () -> Void func body(content: Content) -> some View { content.background(UIViewControllerResponder(viewWillAppear: callback)) } } private struct UIViewControllerResponder: UIViewControllerRepresentable { /// Notifies the view controller that its view is about to be added to a view hierarchy. var viewWillAppear: (() -> Void)? = nil /// Notifies the view controller that its view is about to be removed from a view hierarchy. var viewWillDisappear: (() -> Void)? = nil private let controller = ViewController() func makeUIViewController(context: UIViewControllerRepresentableContext<UIViewControllerResponder>) -> UIViewController { if let viewWillAppear = viewWillAppear { controller.viewWillAppear = viewWillAppear } if let viewWillDisappear = viewWillDisappear { controller.viewWillDisappear = viewWillDisappear } return controller } func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<UIViewControllerResponder>) { } /// An object that manages a view hierarchy for your UIKit app. private class ViewController: UIViewController { var viewWillAppear: () -> Void = { } var viewWillDisappear: () -> Void = { } /// Notifies the view controller that its view is about to be added to a view hierarchy. /// - Parameter animated: If true, the view is being added to the window using an animation. override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) viewWillAppear() } /// /// Notifies the view controller that its view is about to be removed from a view hierarchy. /// - Parameter animated: If true, the view is being added to the window using an animation. override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) viewWillDisappear() } } } And in the View, this is how you interact with the events: struct MyView: View { var body: some View { NavigationStack { Text("Home") .navigationTitle("Some Text") .toolbar { ToolbarItem(placement: .primaryAction) { Button { print("Toolbar button pressed") } label: { Image(systemName: "plus") } } } } .onWillAppear { let appearance = UINavigationBarAppearance() appearance.titleTextAttributes = [.foregroundColor: UIColor.white] appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white] UINavigationBar.appearance().standardAppearance = appearance UINavigationBar.appearance().compactAppearance = appearance } .onWillDisappear { let appearance = UINavigationBarAppearance() appearance.titleTextAttributes = [:] appearance.largeTitleTextAttributes = [:] UINavigationBar.appearance().standardAppearance = appearance UINavigationBar.appearance().compactAppearance = appearance } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’24
Reply to Array of Protocols and Subscript
Hi Claude31 I thought this might have to be the solution and of course it works. I did try making enum ServiceType: Int and doing repairs.allowRepairs[ServiceType.boat] which also worked so long as the dictionary value matched the index of the enum. So another issue will be JSON codable of Dictionary[ServiceType: ServiceDescriptor], all sorts of error here. Wouldn't happen to have some ideas around codable of a Dictionary[Enum: Protocols]?
Topic: App & System Services SubTopic: General Tags:
Replies
Boosts
Views
Activity
Apr ’22
Reply to XCTest Ambiguous type name
I managed to get the errors to go away. I "unchecked" DefaultValue+Extension.swift and DefaultValuePropertyWrapper.swift from being part of the test target and in the DefaultValueTest.swift file replaced @testable import MyFramework with import MyFramework. Compiles and the tests run successfully now 😅
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jan ’22
Reply to HStack Alignments
After some more trial and error I finally got the look I was after. HStack padding seemed to cause the issue. Here is the final result. import SwiftUI struct ContentView: View {     var body: some View {         VStack(alignment: .center, spacing: 64) {             Text("About My Data")                 .font(.title)                 .fontWeight(.heavy)                 .multilineTextAlignment(.center)                 .padding()             VStack(alignment: .leading, spacing: 32) {                 HStack {                     Image(systemName: "network")                             .font(.system(size: 36.0))                             .foregroundColor(.blue)                     VStack(alignment: .leading) {                        Text("Network Support")                         .font(.headline)                         .fontWeight(.bold)                     Text("Download your data from anywhere.")                         .font(.body)                         .foregroundColor(.gray)                     }                 }                 HStack {                     Image(systemName: "hand.raised.fill")                         .font(.system(size: 36.0))                         .foregroundColor(.blue)                     VStack(alignment: .leading) {                         Text("Data Privacy")                         .font(.headline)                         .fontWeight(.bold)                     Text("Scanned certificates are never stored on the device.")                         .font(.body)                         .foregroundColor(.gray)                     }                 }             }         }.padding()     } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Dec ’21
Reply to HStack Alignments
@robnotyou, thanks for replying. I'm trying to achieve making the HStack full width with the image and the VStack text in both containers leading aligned. Because of the text size difference it's slight off.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Dec ’21