Post

Replies

Boosts

Views

Activity

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:
3w
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
3w
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 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 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 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 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 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 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 SwiftUI Trap change of orientation
In that case, don't give up so easily. When working with background images you don't really need to worry about device orientation. SwiftUI can let you resize the image and scale it to fill the available space. Something like this should work: struct BackgroundImageView: View { var body: some View { ZStack { // Background image Image(.background) .resizable() // make sure image can be resized .scaledToFill() // scale the image so it fills all available space .ignoresSafeArea() // [optional] Makes the image fill the whole screen including going behind the clock and battery (top) and the home bar (bottom) // Other content goes here Text("Main Content") } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24
Reply to SwiftUI Trap change of orientation
I am unsure of what you're trying to accomplish. Which do you want to do? Detect when the user rotates their device – in terms of portrait and landscape orientation. Detect when the physical size of your app changes – in terms of size classes. Option 1 isn't recommended in cases such as Split View. For example, an iPad may be in landscape orientation but the width of your app isn't the full width of the device, only a portion of it. Option 2 uses predefined values that tells you whether your app is horizontally (width) or vertically (height) "compact", so you can configure your UI accordingly. I suggest you have a look at the documentation, especially for SwiftUI. You have some concepts about views, data models and communicating between them incorrect. Also check out this guide on layout; the "Device size classes" section might give you a better understanding into what "compact" and "regular" size classes mean.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24
Reply to SwiftUI Orientation Change
The issue you have is that the if statement is placed inline inside the class and Swift doesn't know what to do with it. This is where variable and function declarations go and not code blocks or expressions. Instead, I think you want to put it in an initialiser, like this: init() { if myHorizClass == .compact && myVertClass == .regular { self.myOrient = "Portrait" } else if myHorizClass == .regular && myVertClass == .compact { self.myOrient = "Landscape" } else { self.myOrient = "Something Else" } } Also, I don't know if it was a typo or not, but in the if statement you had elseif instead of else if. Does this solve your problem?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24
Reply to SwiftUI: Cannot find '$lektion' in scope
The toggle requires a Binding to a boolean value but your lektion variable cannot provide one. The solution is to use the Bindable property wrapper like this: ForEach(lektionen) { lektion in @Bindable var lektion = lektion // add this line NavigationLink { VokabelnView(lektion: lektion).navigationTitle("Vokabeln") } label: { Toggle("", isOn: $lektion.isOn) } } Now you can access $lektion which can create bindings to its properties.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’23