Post

Replies

Boosts

Views

Activity

Reply to SwiftUI inspector full height
I believe you need to place the inspector modifier outside of a navigation structure (NavigationStack or NavigationSplitView), which I think was mentioned in the session video. Although you wouldn't necessarily use one for macOS, it's probably needed to have the full-height behaviour. This works for me: struct ContentView: View { var body: some View { NavigationStack { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") } .padding() .toolbar { Text("test") } } .inspector(isPresented: .constant(true)) { Text("this is a test") } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’23
Reply to Disabled button in SwiftUI .alert not working
I encountered a similar issue at the release of iOS 16 which is when I filed a feedback report. At that time, buttons that were disabled didn't show up in the alert at all. Then, sometime during the early iOS 17 betas (beta 4 maybe?), the issue was addressed and this was added to the SwiftUI release notes: Resolved Issues Fixed an issue where dynamically enabled buttons (e.g. with a TextField) were not updated in alerts. (95917673) (FB10463211) I then had to test this to see if things were fixed, and the disabled buttons did show up…but the action closure wasn't called at all. I then filed another feedback report (FB12857555) and nothing has happened yet. I suggest also filing feedback as that will help elevate the problem with Apple and hopefully get it fixed soon.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’23
Reply to How to create window without border between title bar and content view?
This style of window is without the title bar. You would need to remove/hide it to get this effect. In SwiftUI, you would add this modifier to your main App: WindowGroup { ContentView() } .windowStyle(.hiddenTitleBar) // add this modifier here However, it seems like you are using the AppKit lifecycle which means you will have to do this: myWindow = NSWindow( contentRect: NSRect(x: 0, y: 0, width: 200, height: 200), styleMask: [.closable, .titled, .fullSizeContentView], // can make the contentView consume the full size of the window backing: .buffered, defer: false) // Modify these two properties myWindow.titleVisibility = .hidden myWindow.titlebarAppearsTransparent = true
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’23
Reply to SwifUI - Set frame on Group of Buttons not working as expected
The issue is not with the Group but with the order of the modifiers. The order is very important in SwiftUI as it dictates how views are rendered and can produce different outcomes by just flipping two modifiers around. In your case, you are applying the frame modifier after the background modifier which is why you are seeing this result: To explain, these two snippets are effectively the same and demonstrates what Group is doing: Group { Button { } label: { Text("One") .padding() } .background(.black) .tint(.white) Button { } label: { Text("Two") .padding() } .background(.white) .tint(.black) } .frame(minWidth: 0, maxWidth: .infinity) // <- Button { } label: { Text("One") .padding() } .background(.black) .tint(.white) .frame(minWidth: 0, maxWidth: .infinity) // <- Button { } label: { Text("Two") .padding() } .background(.white) .tint(.black) .frame(minWidth: 0, maxWidth: .infinity) // <- What you really want is this: which can be achieved by applying the frame modifier before the background modifier, like this: Button { } label: { Text("One") .padding() } .frame(minWidth: 0, maxWidth: .infinity) // <- .background(.black) .tint(.white) Button { } label: { Text("Two") .padding() } .frame(minWidth: 0, maxWidth: .infinity) // <- .background(.white) .tint(.black) This has to be done individually as using Group can't fulfil that. What you can do instead is create a custom button style with this max frame (and anything else you want) and then apply that to the Group or HStack.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’23
Reply to SwiftUI Map Annotations
Since you already have the visible region shown on the map and all annotations, with their coordinates, you can work out which annotations are located in that region. MKMapRect has a useful method contains(_:) which indicates whether the specified map point lies within the rectangle. There are two (small?) problems though: Map expects annotations to have location coordinates of type CLLocationCoordinate2D but MKMapRect uses MKMapPoint. Luckily, an MKMapPoint can be formed from a CLLocationCoordinate2D. I'm assuming you are storing your map's region as an MKCoordinationRegion object inside of an @State variable. If so, this needs to be converted to an MKMapRect object and that isn't an easy process. If you don't require using MKCoordinationRegion, then store an MKMapRect instead which will make things easier. If you do need it, you will need to manually convert (a quick search online will yield some results), and don't forget about the 180th meridian. [Note: iOS 17 doesn't make Problem 2 an issue, with the new APIs handling things differently.] Here's a demo app I made to showcase how you can achieve this: // A single map annotation as an object struct AnnotationItem: Identifiable { let id = UUID() let title: String let coordinates: CLLocationCoordinate2D } struct MapAnnotations: View { // Store the map's currently visible rect @State private var visibleRect = MKMapRect(x: 125_000_000, y: 75_000_000, width: 15_000_000, height: 25_000_000) // Dummy data let items: [AnnotationItem] = [ .init(title: "San Francisco", coordinates: .init(latitude: 37.77938, longitude: -122.41843)), .init(title: "New York", coordinates: .init(latitude: 40.71298, longitude: -74.00720)), .init(title: "São Paulo", coordinates: .init(latitude: -23.57964, longitude: -46.65506)), .init(title: "London", coordinates: .init(latitude: 51.50335, longitude: -0.07940)), .init(title: "Rome", coordinates: .init(latitude: 41.88929, longitude: 12.49355)), .init(title: "Johannesburg", coordinates: .init(latitude: -26.20227, longitude: 28.04363)), .init(title: "Mumbai", coordinates: .init(latitude: 18.94010, longitude: 72.83466)), .init(title: "Tokyo", coordinates: .init(latitude: 35.68951, longitude: 139.69170)), .init(title: "Melbourne", coordinates: .init(latitude: -37.81503, longitude: 144.96634)) ] var body: some View { Map(mapRect: $visibleRect, annotationItems: items) { item in MapMarker(coordinate: item.coordinates) } .ignoresSafeArea() .overlay(alignment: .bottom) { let annotations = visibleAnnotations() // Show the list of visible annotations if there are any if !annotations.isEmpty { VStack { ForEach(annotations) { item in Text(item.title) .font(.title3.bold()) } } .padding(10) .background(.thinMaterial, in: RoundedRectangle(cornerRadius: 10)) .padding() } } } func visibleAnnotations() -> [AnnotationItem] { items.filter { item in // Check if the item's location is in the map's currently visible rect visibleRect.contains(.init(item.coordinates)) } } } Any questions about this please let me know and I'll be happy to answer.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’23
Reply to How to change the background behind a presented view
You can use the presentationBackground(_:) modifier. Here is an example of how you can use it: .sheet(...) { SheetView() .presentationBackground(.red.opacity(0.5)) // pass in a colour or even a custom view } Note: the translucency of the sheet background means content behind the sheet presentation will show through. If you don't want this behaviour use the regular background(_:ignoresSafeAreaEdges:) modifier with a maximum frame.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’23
Reply to How to retrieve values set in Hashable paths when using a router to navigate through views
You will need to extract the id property from the Form case value using Swift's pattern matching capabilities. Here is an example of how you could achieve this: // Add a computed property to the view var formID: String { if case .Form(let id) = router.paths.last { id } else { "No ID" // handle the case where the ID couldn't be extracted } } // Use this id property as a string Text(formID)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’23
Reply to Weird @Binding behavior, doesn't update the value until I set it into different one
I remember seeing this somewhere before. The issue is that the contents of the sheet are initially created with the initial value (0) before the sheet is first shown. Subsequent sheet presentations then use the new changed value. The solution to this is to capture the current variable's value as the sheet is about to be shown. Here is how you would do that: .sheet(isPresented: $bindingValueBool, content: { [bindingValueInt] in // use the captured value which will be correct Text("This is the selected number \(bindingValueInt)") }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’23
Reply to customized Navigation bar
There is a solution that dips into UIKit which will set the navigation bar's back button to display only the indicator image (chevron) without the title. // Requires iOS 14.0+ extension UINavigationController { open override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() navigationBar.topItem?.backButtonDisplayMode = .minimal } } Note, however, that this will remove the title from every back button in your app.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’23
Reply to How do I filter @Query with bound item
In your Chore model class, you have the property assignedTo of type FamilyMember. Because you are using SwiftData, an implicit relationship is created. However, in order for the relationship to be complete it needs to have an inverse property in the other model. You need to add a property to FamilyMember that can be that inverse. Here's an example of what it could look like: @Model class Chore { @Relationship(inverse: \FamilyMember.chores) var assignedTo: FamilyMember } @Model class FamilyMember { @Relationship var chores: [Chore] } You could remove the Relationship macros and just have that chores property by itself and let SwiftData figure out the relationships, but I haven't tried this yet; it's best to do it yourself, plus you can visually see where the relationships are. In my model classes, relationships are marked as optional, but that's just to satisfy CloudKit. I don't know if they have to be optional regardless (or have default values), but if you run into issues try doing that. Also, I am just guessing that the lack of an inverse property is your problem based off of your code. I haven't tested anything so feel free to let me know if I'm talking about the right stuff.
Sep ’23
Reply to SwiftData: how to reference same model?
The inverse of a SwiftData relationship cannot point back to itself. This is what the circular reference error is referring to. You need to add another property that can be used for the inverse relationship. You don't have to use this property, it's just to satisfy SwiftData's relationship requirements. For example: @Model final class Person { var name = "" @Relationship(deleteRule: .nullify, inverse: \Person.inverseRef) var ref: Person? = nil // Call this whatever you want // Can make private if not using elsewhere @Relationship var inverseRef: Person? } Testing this and adding dump(peter.ref) after deleting cristina yields this output: Optional(Person) ▿ some: Person #0 - _name: Person._SwiftDataNoType ▿ _ref: Optional(Person._SwiftDataNoType()) - some: Person._SwiftDataNoType - _inverseRef: nil ... So peter.ref is not nil but it seems to be an "empty" model with "empty" values, essentially nullified. Whether this is the behaviour you want, I don't know, but I hope it fixes your inverse relationship problem. BTW, nullify is the default delete rule for relationships so it doesn't need specifying.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’23
Reply to iOS 17b6: Simultaneous accesses to ..., but modification requires exclusive access crash using Observation and SwiftUI
From the iOS & iPadOS 17 Beta 8 Release Notes: SwiftUI Known Issues On iOS, using an Observable object’s property as a selection value of a List inside NavigationSplitView may cause a “Simultaneous accesses to …” error when a list selection is made via tap gesture. (113978783) Workaround: There is no current workaround for Observable properties. Alternatives include factoring out the selection value into separate state stored outside the object, or using ObservableObject instead. Your best bet is to wait for the next beta (or RC) and try again with that. I would only use the workaround if you need a fix right now.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’23