Post

Replies

Boosts

Views

Activity

largeTitleDisplayMode doesn't work when using UIHostingController
When using a UIHostingController to host my SwiftUI content, I can't get the navigation title to start large and shift to inline as the user scrolls. Is this a known problem with using the UIHostingController? var viewModel: DashboardViewModel! lazy var contentView = UIHostingController(rootView: DashboardView(viewModel: viewModel)) override func viewDidLoad() { super.viewDidLoad() addChild(contentView) view.addSubview(contentView.view) setupConstraints() navigationItem.title = "Test Title" navigationItem.largeTitleDisplayMode = .automatic } The title in the navigation controller shows inline
4
1
2.1k
Apr ’23
How do I preview a View that's using @Bindable?
I have a SwiftUI view that is being passed a binding from a parent view. I would like to preview the subview, but can't. I tried .constant and a @Model object directly. Both ways crash the preview. Any idea on how I can get this to work properly? @Model class FamilyMember: Identifiable { @Attribute(.unique) var id: String { name } var name = "" init(name: String = "") { self.name = name } } struct MemberView: View { @Bindable var member: FamilyMember var body: some View { Text(member.name) } } #Preview { MemberView(member: FamilyMember(name: "Family member")) } #Preview { MemberView(member: .constant(FamilyMember(name: "Family member"))) }
3
1
3.5k
Sep ’23
Swap rootViewController with animation?
I'm trying to swap out the rootViewController in my appDelegate with an animation and it is just flashing to the new viewController without any animation.-(void)showRootController:(UIViewController *)controller { [UIView transitionWithView:self.window duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ self.window.rootViewController = controller; [self.window makeKeyAndVisible]; } completion:nil]; }How can I do this with an animation? (Testing on an iPhone 5s with iOS 8)
Topic: UI Frameworks SubTopic: UIKit Tags:
3
0
7.5k
Aug ’21
How to use dependencies in a Swift Package
I created a new Package with Xcode and incorporated a dependency, however when I try to use it, I get a "No such module" error. How do I use the dependency in the Package sources? In a normal project, I can easily import and use AgileDB. Here's the Package: // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription let package = Package(     name: "DBCore",     products: [         // Products define the executables and libraries a package produces, and make them visible to other packages.         .library(             name: "DBCore",             targets: ["DBCore"]),     ],     dependencies: [   .package(url: "https://github.com/AaronBratcher/AgileDB", from: "6.4.0")     ],     targets: [         // Targets are the basic building blocks of a package. A target can define a module or a test suite.         // Targets can depend on other targets in this package, and on products in packages this package depends on.         .target(             name: "DBCore",             dependencies: []),         .testTarget(             name: "DBCoreTests",             dependencies: ["DBCore"]),     ] ) Perhaps the AgileDB package as a dependency in the target? I tried copying that and it won't recognize it.
1
0
1.1k
Apr ’22
Xcode 14 NavigationPath – two views pushed onto nav stack when multiple links in form section
Create new SwiftUI project Replace ContentView with the code below Tap either link. Both views are pushed onto the nav stack class ContentViewModel: ObservableObject { @Published var navigationPath = NavigationPath() } struct ContentView: View { @StateObject var viewModel = ContentViewModel() var body: some View { NavigationStack(path: $viewModel.navigationPath) { Form { Section { VStack { NavigationLink(value: 1) { Text("Location") } NavigationLink(value: 2) { Text("Category") } } } }.navigationDestination(for: Int.self) { route in switch route { case 1: Text("Location") case 2: Text("Category") default: Text("Unknown") } }.navigationTitle("Home") } } } What am I doing wrong? I would only expect the tapped item to be pushed into the stack.
1
0
1.2k
Oct ’22
Is SwiftUI Picker not compatible with a Bindable property?
I have a view with @Bindable property. (It's a model) I can use TextField("Name", text: $chore.name), but not Picker(selection: $chore.choreFrequency, label: EmptyView()) The auto-complete doesn't even show choreFrequency as an option enum ChoreFrequency: String, CaseIterable, Identifiable, Codable { case daily, weekly, monthly, seasonal var id: ChoreFrequency { self } } @Model class Chore: Identifiable { @Attribute(.unique) var id = UUID() var name: String var frequency: ChoreFrequency var assignedTo: FamilyMember var isComplete: Bool var dueDate: Date } Can I not use the Bindable property in the Picker?
3
0
1.4k
Sep ’23
Is there a cleaner way of making an enum with associated value conform to rawRepresentable?
I have this in my code and it works, however if I have a long list it gets tiresome. Is there a better way of having an enum with associated values that also conforms to RawRepresentable? public enum IconColor { case regular case error case warning case success case custom(String) public var value: Color { return loadColor(self.rawValue) } } extension IconColor: RawRepresentable { public var rawValue: String { switch self { case .regular: return "icon_regular" case .error: return "icon_error" case .warning: return "icon_warning" case .success: return "icon_success" case .custom(let value): return value } } public init(rawValue: String) { switch rawValue { case "icon_regular": self = .regular case "icon_error": self = .error case "icon_warning": self = .warning case "icon_success": self = .success default: self = .custom(rawValue) } } }
3
0
965
Sep ’22
How do I get updated custom EnvironmentValues after a method call?
I have a custom EnvironmentVariable called analyticsData. On the button tap, I want to perform my button's action and then call a method to send this data. struct PrimaryButton: View { @Environment(\.analyticsData) private var analyticsData private let title: String private let action: (() -> Void) init(_ title: String, action: @escaping (() -> Void)) { self.title = title self.action = action } var body: some View { Button(action: handleTap) { Text(title) } } func handleTap() { action() // need updated value of analyticsData here! AnalyticsProxy.shared.sendAnalytics(analyticsData) } } At the point of use for this PrimaryButton, I would like to update my data dynamically. However, tapping between my 2 buttons, the data is 1 click behind. button 1 tapped, analytics output: buttonTapped = "" button 2 tapped, analytics output: buttonTapped = "B1" button 1 tapped, analytics output: buttonTapped = "B2" In my PrimaryButton handleTap method (above), I need to get the updated value of the environmentVariable before sending the analytics. Is this possible? Point of use: struct ContentView: View { @State var buttonTapped = "" var body: some View { VStack(spacing: 64) { PrimaryButton("Button 1") { buttonTapped = "B1" }.analytics(type: .event, name: "Button Tap", data: ["Button": buttonTapped]) PrimaryButton("Button 2") { buttonTapped = "B2" }.analytics(type: .event, name: "Button Tap", data: ["Button": buttonTapped]) } } }
2
0
929
Jul ’22
Why is rotationEffect moving vertically too?
I have my animation for the rotation working beautifully, but the image is also moving vertically. Why? struct SyncView: View { @State var isSyncing = false let animation: Animation = Animation.linear(duration: 2.0).repeatForever(autoreverses: false) var body: some View { VStack(spacing: 16) { HStack { VStack(alignment: .leading) { Text("Linked Device's Name") Text("Updating (Last update: Oct 1, 2022)") .font(.callout) .foregroundColor(.gray) } Spacer() Text(Image(systemName: "arrow.triangle.2.circlepath")) .foregroundStyle(Color.yellow) .font(.title) .rotationEffect(Angle.degrees(isSyncing ? 360 : 0)) .animation(animation, value: isSyncing) } } .padding() .onAppear { isSyncing = true } } }
1
0
1k
Nov ’22
Associated Code?
When showing an ImageDownloader actor, the presenter said a solution on avoiding duplicate downloads is shown with the code associated with this video. When will this code be available? I don't see it on the video page.
Replies
6
Boosts
0
Views
2.9k
Activity
Jan ’22
largeTitleDisplayMode doesn't work when using UIHostingController
When using a UIHostingController to host my SwiftUI content, I can't get the navigation title to start large and shift to inline as the user scrolls. Is this a known problem with using the UIHostingController? var viewModel: DashboardViewModel! lazy var contentView = UIHostingController(rootView: DashboardView(viewModel: viewModel)) override func viewDidLoad() { super.viewDidLoad() addChild(contentView) view.addSubview(contentView.view) setupConstraints() navigationItem.title = "Test Title" navigationItem.largeTitleDisplayMode = .automatic } The title in the navigation controller shows inline
Replies
4
Boosts
1
Views
2.1k
Activity
Apr ’23
Xcode 16 preview not working
I want to add SwiftUI to an existing package. Super simple to get started. It previews with Xcode 15.4, but gives an error on Xcode 16.0: JITError: Runtime linking failure Anyone have an idea on what may be failing? Full SwiftUI code: import SwiftUI struct Dashboard: View { var body: some View { Text("hello") } } #Preview { Dashboard() }
Replies
4
Boosts
3
Views
4k
Activity
Sep ’24
Swift Data in Background Processing?
Is it possible to do Swift Data operations on a background processing event?
Replies
0
Boosts
2
Views
544
Activity
Aug ’23
How do I preview a View that's using @Bindable?
I have a SwiftUI view that is being passed a binding from a parent view. I would like to preview the subview, but can't. I tried .constant and a @Model object directly. Both ways crash the preview. Any idea on how I can get this to work properly? @Model class FamilyMember: Identifiable { @Attribute(.unique) var id: String { name } var name = "" init(name: String = "") { self.name = name } } struct MemberView: View { @Bindable var member: FamilyMember var body: some View { Text(member.name) } } #Preview { MemberView(member: FamilyMember(name: "Family member")) } #Preview { MemberView(member: .constant(FamilyMember(name: "Family member"))) }
Replies
3
Boosts
1
Views
3.5k
Activity
Sep ’23
SwiftUI image from Bundle.main not working
I have a project with a single asset image. In the dependency package, I am attempting to preview using the image in the project using Bundle.main. I just get a blank. How can I get this to work? Sample project here: https://github.com/AaronBratcher/SwiftUIPreviewProblem
Replies
6
Boosts
1
Views
1.3k
Activity
Oct ’24
Swap rootViewController with animation?
I'm trying to swap out the rootViewController in my appDelegate with an animation and it is just flashing to the new viewController without any animation.-(void)showRootController:(UIViewController *)controller { [UIView transitionWithView:self.window duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ self.window.rootViewController = controller; [self.window makeKeyAndVisible]; } completion:nil]; }How can I do this with an animation? (Testing on an iPhone 5s with iOS 8)
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
3
Boosts
0
Views
7.5k
Activity
Aug ’21
How to use dependencies in a Swift Package
I created a new Package with Xcode and incorporated a dependency, however when I try to use it, I get a "No such module" error. How do I use the dependency in the Package sources? In a normal project, I can easily import and use AgileDB. Here's the Package: // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription let package = Package(     name: "DBCore",     products: [         // Products define the executables and libraries a package produces, and make them visible to other packages.         .library(             name: "DBCore",             targets: ["DBCore"]),     ],     dependencies: [   .package(url: "https://github.com/AaronBratcher/AgileDB", from: "6.4.0")     ],     targets: [         // Targets are the basic building blocks of a package. A target can define a module or a test suite.         // Targets can depend on other targets in this package, and on products in packages this package depends on.         .target(             name: "DBCore",             dependencies: []),         .testTarget(             name: "DBCoreTests",             dependencies: ["DBCore"]),     ] ) Perhaps the AgileDB package as a dependency in the target? I tried copying that and it won't recognize it.
Replies
1
Boosts
0
Views
1.1k
Activity
Apr ’22
How do I show code coverage in Swift Packages?
On my M1 mac, using Xcode 13.3, I created a package and displayed the code coverage bar (Editor menu –> Code Coverage). After running tests, there is no indication of code coverage at all in the source code. How do I get code coverage when testing a package?
Replies
1
Boosts
0
Views
1.1k
Activity
Apr ’22
Xcode 14 NavigationPath – two views pushed onto nav stack when multiple links in form section
Create new SwiftUI project Replace ContentView with the code below Tap either link. Both views are pushed onto the nav stack class ContentViewModel: ObservableObject { @Published var navigationPath = NavigationPath() } struct ContentView: View { @StateObject var viewModel = ContentViewModel() var body: some View { NavigationStack(path: $viewModel.navigationPath) { Form { Section { VStack { NavigationLink(value: 1) { Text("Location") } NavigationLink(value: 2) { Text("Category") } } } }.navigationDestination(for: Int.self) { route in switch route { case 1: Text("Location") case 2: Text("Category") default: Text("Unknown") } }.navigationTitle("Home") } } } What am I doing wrong? I would only expect the tapped item to be pushed into the stack.
Replies
1
Boosts
0
Views
1.2k
Activity
Oct ’22
SwiftData Aggregation
Given a simple class: final class Person { name: String age: Int } How can I get aggregated data such as average age, or count of people within specific ranges without retrieving all the data and doing the aggregates myself?
Replies
2
Boosts
0
Views
1.4k
Activity
Aug ’23
Is SwiftUI Picker not compatible with a Bindable property?
I have a view with @Bindable property. (It's a model) I can use TextField("Name", text: $chore.name), but not Picker(selection: $chore.choreFrequency, label: EmptyView()) The auto-complete doesn't even show choreFrequency as an option enum ChoreFrequency: String, CaseIterable, Identifiable, Codable { case daily, weekly, monthly, seasonal var id: ChoreFrequency { self } } @Model class Chore: Identifiable { @Attribute(.unique) var id = UUID() var name: String var frequency: ChoreFrequency var assignedTo: FamilyMember var isComplete: Bool var dueDate: Date } Can I not use the Bindable property in the Picker?
Replies
3
Boosts
0
Views
1.4k
Activity
Sep ’23
Is there a cleaner way of making an enum with associated value conform to rawRepresentable?
I have this in my code and it works, however if I have a long list it gets tiresome. Is there a better way of having an enum with associated values that also conforms to RawRepresentable? public enum IconColor { case regular case error case warning case success case custom(String) public var value: Color { return loadColor(self.rawValue) } } extension IconColor: RawRepresentable { public var rawValue: String { switch self { case .regular: return "icon_regular" case .error: return "icon_error" case .warning: return "icon_warning" case .success: return "icon_success" case .custom(let value): return value } } public init(rawValue: String) { switch rawValue { case "icon_regular": self = .regular case "icon_error": self = .error case "icon_warning": self = .warning case "icon_success": self = .success default: self = .custom(rawValue) } } }
Replies
3
Boosts
0
Views
965
Activity
Sep ’22
How do I get updated custom EnvironmentValues after a method call?
I have a custom EnvironmentVariable called analyticsData. On the button tap, I want to perform my button's action and then call a method to send this data. struct PrimaryButton: View { @Environment(\.analyticsData) private var analyticsData private let title: String private let action: (() -> Void) init(_ title: String, action: @escaping (() -> Void)) { self.title = title self.action = action } var body: some View { Button(action: handleTap) { Text(title) } } func handleTap() { action() // need updated value of analyticsData here! AnalyticsProxy.shared.sendAnalytics(analyticsData) } } At the point of use for this PrimaryButton, I would like to update my data dynamically. However, tapping between my 2 buttons, the data is 1 click behind. button 1 tapped, analytics output: buttonTapped = "" button 2 tapped, analytics output: buttonTapped = "B1" button 1 tapped, analytics output: buttonTapped = "B2" In my PrimaryButton handleTap method (above), I need to get the updated value of the environmentVariable before sending the analytics. Is this possible? Point of use: struct ContentView: View { @State var buttonTapped = "" var body: some View { VStack(spacing: 64) { PrimaryButton("Button 1") { buttonTapped = "B1" }.analytics(type: .event, name: "Button Tap", data: ["Button": buttonTapped]) PrimaryButton("Button 2") { buttonTapped = "B2" }.analytics(type: .event, name: "Button Tap", data: ["Button": buttonTapped]) } } }
Replies
2
Boosts
0
Views
929
Activity
Jul ’22
Why is rotationEffect moving vertically too?
I have my animation for the rotation working beautifully, but the image is also moving vertically. Why? struct SyncView: View { @State var isSyncing = false let animation: Animation = Animation.linear(duration: 2.0).repeatForever(autoreverses: false) var body: some View { VStack(spacing: 16) { HStack { VStack(alignment: .leading) { Text("Linked Device's Name") Text("Updating (Last update: Oct 1, 2022)") .font(.callout) .foregroundColor(.gray) } Spacer() Text(Image(systemName: "arrow.triangle.2.circlepath")) .foregroundStyle(Color.yellow) .font(.title) .rotationEffect(Angle.degrees(isSyncing ? 360 : 0)) .animation(animation, value: isSyncing) } } .padding() .onAppear { isSyncing = true } } }
Replies
1
Boosts
0
Views
1k
Activity
Nov ’22