Post

Replies

Boosts

Views

Activity

Reply to phaseAnimator: prevent opacity change?
Could you not use two separate state variables? This code works: struct ContentView: View { @State private var value: Int = 0 @State private var trigger = 0 var body: some View { VStack { GeometryReader { geometry in Text("\(value)") .phaseAnimator([0, 1], trigger: trigger) { view, phase in view .offset(x: phase == 1 ? 100 : 0) } animation: { phase in switch phase { case 1: .linear(duration: 1) default: nil } } } Button("Start Animation") { withAnimation { trigger += 1 } completion: { value += 1 } } } } } The trigger property is for triggering the animation. The value property only gets updated when the animation is complete, i.e. on the "snap back". This way the trigger and what is shown in the view cannot interfere with each other. It also ensures the text value can be updated after the animation, not before, like you said you wanted. If this solution is something you're not looking for, or there is more surrounding this problem, then I am still here to help. Just let me know.
Topic: UI Frameworks SubTopic: SwiftUI
May ’24
Reply to How to use async functions with WithAnimation
Since you haven't provided any code samples, I can only guess at what you're attempting to do. This code will work: struct ContentView: View { @State private var fetchedData: [MyModel] = [] var body: some View { List(fetchedData) { model in Text(model.name) } .task(priority: .background) { let data = try? await modelActor.fetchData() await MainActor.run { withAnimation { fetchedData = data } } } } } It fetches the data asynchronously on a background thread, and then updates the UI with this new data, with an animation and on the main actor. If this is not what you're looking for, or there is more to your issue, then you need to provide some more context and some code.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24
Reply to Swift: Navigation link not working.
The issue is you are placing the NavigationLink inside of a button's action closure. The first argument of the Button is action of type () -> Void (no parameters, no return). You are placing a NavigationLink inside which isn't being used at all, causing the warning to be generated. To fix this you need to use either the Button or the NavigationLink. As for your second problem, you are placing the NavigationStack inside of a VStack so the other content won't "show properly". Instead, try moving the NavigationStack to the top level. This code should solve both issues: NavigationStack { // move to top ZStack { Image("iPad Background 810 X 1060") .resizable() .scaledToFill() .ignoresSafeArea() VStack { HStack{ Image("Diabetes Control Logo 1024X1024") .resizable() .frame(width: 100, height: 100, alignment: .leading) .padding(.leading, 40) Text("Diabetes Control") .font(Font.custom("SnellRoundhand-Black", size: 40)) .background(Color(red: 255, green: 253, blue: 208)) .shadow(color: Color(red: 128, green: 128, blue: 128), radius: 3) .padding(.leading, 150) Spacer() }.padding(.bottom, 35) HStack { Text("What would you like to do : ") .font(Font.custom("SnellRoundhand-Black", size: 30)) .background(Color(red: 128, green: 128, blue: 128)) .shadow(color: Color(red: 126, green: 128, blue: 128), radius: 3) Spacer() }.padding(.leading, 35) .padding(.bottom,35) HStack { NavigationLink { // change to use only navlink iPadReadingsEntry() } label: { Text("Enter a BLOOD Glucose reading") }.background(Color(red: 255, green: 253, blue: 208)) .foregroundColor(.black) .font(Font.custom("SnellRoundhand", size: 24)) Spacer() }.padding(.leading, 60) .padding(.bottom, 25) Spacer() }.background(.blue) Spacer() } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24
Reply to How can I subscribe to changes to an @AppStorage var...
From my knowledge, I believe @AppStorage is more like @State. The projectedValue of the Published property wrapper exposes a publisher which is what you are subscribing to. The projectedValue of the AppStorage property wrapper is a binding to a value from UserDefaults. Note: the projectedValue is the property accessed with the $ operator. As a solution, I would use SwiftUI's onChange(of:perform:) modifier, or I guess as an alternative didSet, but this seems to be not what you want. I am not familiar enough with Combine to provide a full working solution, but I can give you some ideas: Create a custom Publisher as an extension on AppStorage to subscribe to Could create a custom property wrapper which replicates AppStorage but has a publisher for its projectedValue Or, could send/publish the change in the didSet of the @AppStorage property using some Combine object
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24
Reply to How to set timeFormat of DatePicker in swift
How are you setting your locale? If you aren't already, you can set it through the environment, like this: DatePicker(...) .environment(\.locale, myCustomLocale) You can customise a Locale object with the values you want for specified properties, such as the region or calendar.
Topic: UI Frameworks SubTopic: UIKit Tags:
May ’24
Reply to How to set timeFormat of DatePicker in swift
So you're not using SwiftUI. You should probably place this post in the UIKit topic instead so it's less confusing. ‎ In UIKit above property isn't available I believe UIDatePicker has a locale property that you can assign a custom locale to. It will do the same as the SwiftUI solution. If this doesn't work, or you have already tried it, then you need to provide more context and the code you are using so we can understand what is going on.
Topic: UI Frameworks SubTopic: UIKit Tags:
Jun ’24
Reply to Crash using Binding.init?(_: Binding<Value?>)
I have had issues in the past when using the Binding initialisers that accept another Binding. According to the error message, it is force unwrapping the value inside instead of returning nil from the initialiser itself. I find creating your own Binding seems to work for most cases. if let number { InnerView(number: .init { number } set: { self.number = $0 }) }
Topic: UI Frameworks SubTopic: SwiftUI
Mar ’25
Reply to Computed property for Shape within the SwiftUI's views
Hey there, welcome to the forums and SwiftUI! You seem to have come across a common problem in SwiftUI when dealing with protocols, like Shape. You're telling Swift that the shape property is some type that conforms to Shape, but the problem is that Ellipse and RoundedRectangle are two different concrete types — even though they both conform to Shape. Swift needs to know the exact concrete type at compile time, so you can't return two different shape types from the same computed property unless you erase the type. Swift suggests using any Shape, like this: var shape: any Shape { if isEllipsis { Ellipse() } else { RoundedRectangle(cornerRadius: 8) } } But this approach doesn't work directly with modifiers like background(_:in:), because SwiftUI's view builder needs a specific Shape type, not a generic any Shape. The solution is to use AnyShape – a type-erased shape value – and can be used by wrapping each Shape type like this: var shape: some Shape { if isEllipsis { AnyShape(Ellipse()) } else { AnyShape(RoundedRectangle(cornerRadius: 8)) } } Now Swift can infer the concrete type as AnyShape, whilst also preserving the underlying shape's behaviour (like how it draws itself). That gives you flexibility without breaking Swift's strict type system. You could also move the logic into the body and inline the condition using a ternary operator instead, like this: .background(color, in: isEllipsis ? AnyShape(Ellipse()) : AnyShape(RoundedRectangle(cornerRadius: 8))) // You can use the shorter syntax for creating shapes if you want to .background(color, in: isEllipsis ? AnyShape(.ellipse) : AnyShape(.rect(cornerRadius: 8))) I hope this explanation helps and also fixes your problem.
Topic: UI Frameworks SubTopic: SwiftUI
Jun ’25
Reply to How to force soft scrollEdgeEffectStyle in iOS 26?
I believe the solution to this problem is to use the new safeAreaBar(edge:alignment:spacing:content:) view modifier, as suggested by its documentation and a comment made during the SwiftUI group lab. However, I also believe that this modifier is currently broken (as of beta 2), because its behaviour seems to be no different to using safeAreaInset. I have filed feedback for this: FB18350439.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’25
Reply to Crash with NSAttributedString in Core Data
What is your custom transformer class? Since NSAttributedString isn't automatically transformed, you need to create your own one. Something like this will suffice: class AttributedStringToDataTransformer: NSSecureUnarchiveFromDataTransformer { override class var allowedTopLevelClasses: [AnyClass] { [NSAttributedString.self] } override class func transformedValueClass() -> AnyClass { NSAttributedString.self } override class func allowsReverseTransformation() -> Bool { true } } extension NSValueTransformerName { static let attributedStringToDataTransformer = NSValueTransformerName("AttributedStringToDataTransformer") } The name of the transformer you set in the Core Data editor would be "AttributedStringToDataTransformer", and the custom class would be "NSAttributedString". And then you need to register this custom transformer before initialising your persistent container with Core Data. ValueTransformer.setValueTransformer(AttributedStringToDataTransformer(), forName: .attributedStringToDataTransformer) The second point is converting between NSAttributedString and AttributedString. You can make things easier by adding a property of type AttributedString to your Item class that does the conversion. @NSManaged private var attributedString: NSAttributedString? public var notesText: AttributedString { get { attributedString.map(AttributedString.init) ?? "" } set { attributedString = NSAttributedString(newValue) } } You can then use this property directly in SwiftUI, and even bind the text editor to it. For example: @State private var notesText: AttributedString = "" // Load text init(item: Item) { self.item = item self._notesText = State(initialValue: item.notesText) } // Save text func saveButtonTapped() { item.notesText = notesText try? managedObjectContext.save() } Hope this helps and solves your issue.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’25
Reply to How does one know the fitting width of a UIDatePicker in a function hooked up with UIControlEventValueChanged
The main point I'm trying to get across is that you would need to wait for the next layout pass after the call to valueChanged so that the new width is the correct one. So, it's however you would do that normally — either by: 1‎. Waiting for the next update (I'd assume the equivalent to Task would be using GCD, like Swift used to have DispatchQueue.main.async): dispatch_async(dispatch_get_main_queue(), ^{ CGFloat width = [datePicker systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].width; }); 2‎. Forcing a layout update so the width is updated immediately: [datePicker setNeedsLayout]; [datePicker layoutIfNeeded]; CGFloat width = [datePicker systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].width; 3‎. Another method you prefer using. Hope I've explained it better and this solves your issue.
Topic: UI Frameworks SubTopic: UIKit Tags:
Jun ’25
Reply to phaseAnimator: prevent opacity change?
Could you not use two separate state variables? This code works: struct ContentView: View { @State private var value: Int = 0 @State private var trigger = 0 var body: some View { VStack { GeometryReader { geometry in Text("\(value)") .phaseAnimator([0, 1], trigger: trigger) { view, phase in view .offset(x: phase == 1 ? 100 : 0) } animation: { phase in switch phase { case 1: .linear(duration: 1) default: nil } } } Button("Start Animation") { withAnimation { trigger += 1 } completion: { value += 1 } } } } } The trigger property is for triggering the animation. The value property only gets updated when the animation is complete, i.e. on the "snap back". This way the trigger and what is shown in the view cannot interfere with each other. It also ensures the text value can be updated after the animation, not before, like you said you wanted. If this solution is something you're not looking for, or there is more surrounding this problem, then I am still here to help. Just let me know.
Topic: UI Frameworks SubTopic: SwiftUI
Replies
Boosts
Views
Activity
May ’24
Reply to How to use async functions with WithAnimation
Since you haven't provided any code samples, I can only guess at what you're attempting to do. This code will work: struct ContentView: View { @State private var fetchedData: [MyModel] = [] var body: some View { List(fetchedData) { model in Text(model.name) } .task(priority: .background) { let data = try? await modelActor.fetchData() await MainActor.run { withAnimation { fetchedData = data } } } } } It fetches the data asynchronously on a background thread, and then updates the UI with this new data, with an animation and on the main actor. If this is not what you're looking for, or there is more to your issue, then you need to provide some more context and some code.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’24
Reply to Swift: Navigation link not working.
The issue is you are placing the NavigationLink inside of a button's action closure. The first argument of the Button is action of type () -> Void (no parameters, no return). You are placing a NavigationLink inside which isn't being used at all, causing the warning to be generated. To fix this you need to use either the Button or the NavigationLink. As for your second problem, you are placing the NavigationStack inside of a VStack so the other content won't "show properly". Instead, try moving the NavigationStack to the top level. This code should solve both issues: NavigationStack { // move to top ZStack { Image("iPad Background 810 X 1060") .resizable() .scaledToFill() .ignoresSafeArea() VStack { HStack{ Image("Diabetes Control Logo 1024X1024") .resizable() .frame(width: 100, height: 100, alignment: .leading) .padding(.leading, 40) Text("Diabetes Control") .font(Font.custom("SnellRoundhand-Black", size: 40)) .background(Color(red: 255, green: 253, blue: 208)) .shadow(color: Color(red: 128, green: 128, blue: 128), radius: 3) .padding(.leading, 150) Spacer() }.padding(.bottom, 35) HStack { Text("What would you like to do : ") .font(Font.custom("SnellRoundhand-Black", size: 30)) .background(Color(red: 128, green: 128, blue: 128)) .shadow(color: Color(red: 126, green: 128, blue: 128), radius: 3) Spacer() }.padding(.leading, 35) .padding(.bottom,35) HStack { NavigationLink { // change to use only navlink iPadReadingsEntry() } label: { Text("Enter a BLOOD Glucose reading") }.background(Color(red: 255, green: 253, blue: 208)) .foregroundColor(.black) .font(Font.custom("SnellRoundhand", size: 24)) Spacer() }.padding(.leading, 60) .padding(.bottom, 25) Spacer() }.background(.blue) Spacer() } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’24
Reply to How can I subscribe to changes to an @AppStorage var...
From my knowledge, I believe @AppStorage is more like @State. The projectedValue of the Published property wrapper exposes a publisher which is what you are subscribing to. The projectedValue of the AppStorage property wrapper is a binding to a value from UserDefaults. Note: the projectedValue is the property accessed with the $ operator. As a solution, I would use SwiftUI's onChange(of:perform:) modifier, or I guess as an alternative didSet, but this seems to be not what you want. I am not familiar enough with Combine to provide a full working solution, but I can give you some ideas: Create a custom Publisher as an extension on AppStorage to subscribe to Could create a custom property wrapper which replicates AppStorage but has a publisher for its projectedValue Or, could send/publish the change in the didSet of the @AppStorage property using some Combine object
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’24
Reply to How to set timeFormat of DatePicker in swift
How are you setting your locale? If you aren't already, you can set it through the environment, like this: DatePicker(...) .environment(\.locale, myCustomLocale) You can customise a Locale object with the values you want for specified properties, such as the region or calendar.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
May ’24
Reply to How to set timeFormat of DatePicker in swift
So you're not using SwiftUI. You should probably place this post in the UIKit topic instead so it's less confusing. ‎ In UIKit above property isn't available I believe UIDatePicker has a locale property that you can assign a custom locale to. It will do the same as the SwiftUI solution. If this doesn't work, or you have already tried it, then you need to provide more context and the code you are using so we can understand what is going on.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Jun ’24
Reply to Crash using Binding.init?(_: Binding<Value?>)
I have had issues in the past when using the Binding initialisers that accept another Binding. According to the error message, it is force unwrapping the value inside instead of returning nil from the initialiser itself. I find creating your own Binding seems to work for most cases. if let number { InnerView(number: .init { number } set: { self.number = $0 }) }
Topic: UI Frameworks SubTopic: SwiftUI
Replies
Boosts
Views
Activity
Mar ’25
Reply to UITextView in SwiftUI Not Scrolling to Position When Becoming First Responder
Regarding your final point about modifying the space between lines, you could use the lineSpacing(_:) modifier. TextEditor(text: $text) .lineSpacing(20)
Topic: UI Frameworks SubTopic: SwiftUI
Replies
Boosts
Views
Activity
Mar ’25
Reply to Computed property for Shape within the SwiftUI's views
Hey there, welcome to the forums and SwiftUI! You seem to have come across a common problem in SwiftUI when dealing with protocols, like Shape. You're telling Swift that the shape property is some type that conforms to Shape, but the problem is that Ellipse and RoundedRectangle are two different concrete types — even though they both conform to Shape. Swift needs to know the exact concrete type at compile time, so you can't return two different shape types from the same computed property unless you erase the type. Swift suggests using any Shape, like this: var shape: any Shape { if isEllipsis { Ellipse() } else { RoundedRectangle(cornerRadius: 8) } } But this approach doesn't work directly with modifiers like background(_:in:), because SwiftUI's view builder needs a specific Shape type, not a generic any Shape. The solution is to use AnyShape – a type-erased shape value – and can be used by wrapping each Shape type like this: var shape: some Shape { if isEllipsis { AnyShape(Ellipse()) } else { AnyShape(RoundedRectangle(cornerRadius: 8)) } } Now Swift can infer the concrete type as AnyShape, whilst also preserving the underlying shape's behaviour (like how it draws itself). That gives you flexibility without breaking Swift's strict type system. You could also move the logic into the body and inline the condition using a ternary operator instead, like this: .background(color, in: isEllipsis ? AnyShape(Ellipse()) : AnyShape(RoundedRectangle(cornerRadius: 8))) // You can use the shorter syntax for creating shapes if you want to .background(color, in: isEllipsis ? AnyShape(.ellipse) : AnyShape(.rect(cornerRadius: 8))) I hope this explanation helps and also fixes your problem.
Topic: UI Frameworks SubTopic: SwiftUI
Replies
Boosts
Views
Activity
Jun ’25
Reply to iOS 26 Reference to member '.glassProminent' cannot be resolved without a contextual typeSourceKit
Available in Xcode 26 beta 2. https://developer.apple.com/documentation/swiftui/primitivebuttonstyle/glassprominent
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’25
Reply to How to force soft scrollEdgeEffectStyle in iOS 26?
I believe the solution to this problem is to use the new safeAreaBar(edge:alignment:spacing:content:) view modifier, as suggested by its documentation and a comment made during the SwiftUI group lab. However, I also believe that this modifier is currently broken (as of beta 2), because its behaviour seems to be no different to using safeAreaInset. I have filed feedback for this: FB18350439.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’25
Reply to Crash with NSAttributedString in Core Data
What is your custom transformer class? Since NSAttributedString isn't automatically transformed, you need to create your own one. Something like this will suffice: class AttributedStringToDataTransformer: NSSecureUnarchiveFromDataTransformer { override class var allowedTopLevelClasses: [AnyClass] { [NSAttributedString.self] } override class func transformedValueClass() -> AnyClass { NSAttributedString.self } override class func allowsReverseTransformation() -> Bool { true } } extension NSValueTransformerName { static let attributedStringToDataTransformer = NSValueTransformerName("AttributedStringToDataTransformer") } The name of the transformer you set in the Core Data editor would be "AttributedStringToDataTransformer", and the custom class would be "NSAttributedString". And then you need to register this custom transformer before initialising your persistent container with Core Data. ValueTransformer.setValueTransformer(AttributedStringToDataTransformer(), forName: .attributedStringToDataTransformer) The second point is converting between NSAttributedString and AttributedString. You can make things easier by adding a property of type AttributedString to your Item class that does the conversion. @NSManaged private var attributedString: NSAttributedString? public var notesText: AttributedString { get { attributedString.map(AttributedString.init) ?? "" } set { attributedString = NSAttributedString(newValue) } } You can then use this property directly in SwiftUI, and even bind the text editor to it. For example: @State private var notesText: AttributedString = "" // Load text init(item: Item) { self.item = item self._notesText = State(initialValue: item.notesText) } // Save text func saveButtonTapped() { item.notesText = notesText try? managedObjectContext.save() } Hope this helps and solves your issue.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’25
Reply to How does one know the fitting width of a UIDatePicker in a function hooked up with UIControlEventValueChanged
Couldn't you just use the systemLayoutSizeFitting method in the value-changed selector? datePicker.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).width
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Jun ’25
Reply to How does one know the fitting width of a UIDatePicker in a function hooked up with UIControlEventValueChanged
Okay, I think I figured it out. What I didn't realise before when testing systemLayoutSizeFitting was that the width being printed was actually the old width, not the updated one. Just to confirm, I wrapped the print statement in a Task (or you could do a tiny delay), and now it prints the correct width after the change.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Jun ’25
Reply to How does one know the fitting width of a UIDatePicker in a function hooked up with UIControlEventValueChanged
The main point I'm trying to get across is that you would need to wait for the next layout pass after the call to valueChanged so that the new width is the correct one. So, it's however you would do that normally — either by: 1‎. Waiting for the next update (I'd assume the equivalent to Task would be using GCD, like Swift used to have DispatchQueue.main.async): dispatch_async(dispatch_get_main_queue(), ^{ CGFloat width = [datePicker systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].width; }); 2‎. Forcing a layout update so the width is updated immediately: [datePicker setNeedsLayout]; [datePicker layoutIfNeeded]; CGFloat width = [datePicker systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].width; 3‎. Another method you prefer using. Hope I've explained it better and this solves your issue.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Jun ’25