Post

Replies

Boosts

Views

Activity

SwiftUI View incorrectly disabled based on position?
My app has a sort of "HUD" that sits at the top of the screen. It contains some general information about gameplay, and a couple of interactive elements: a scrolling list of text and a button. It's imbedded into the main view like this: struct WindowedInGameView: View { @Environment(GameModel.self) private var gameModel var body: some View { ZStack(alignment: .top) { VStack { HUDTopView() switch(gameModel.game?.location.player_location) { case .plaza: PlazaView() case .city_hall: CityHallView() ///... Lots of additional views... default: Text("Player is in unknown location.") } // --> Position 2 } if gameModel.showConversationView { ... an overlay for NPC conversations... } if (gameModel.showNotificationView) { .. a notification overlay... } } } } If HUDTopView() is at the indicated place at the top of the VStack, none of its interactive elements work: you can't press buttons, you can't scroll the text. Everything looks present and interactive, it just doesn't seem to receive any input. If I move HUDTopView() to the bottom of the VStack (where the comment indicates "Position 2") it works just fine, except it's not where I want it to be located. So far as I can tell, its position alone is disabling it somehow. In either case, any interactive elements in the views from the switch are acting fine, regardless of their position relative to the HUD view. This fails in the same way on iOS, iPadOS, and macOS. (The visionOS implementation doesn't use this main view, so I haven't tried it there). What it isn't: the overlays at the bottom. Commenting them out doesn't change behaviour. the ZStack - I can replace it with an HStack, and it still fails. Anything about the container of this view -- it fails in preview Anything about the HUDTopView() -- a simple button placed there becomes noninteractive, too. I'm at a complete loss for what could possibly be causing this.
Topic: UI Frameworks SubTopic: SwiftUI
1
0
317
Nov ’24
@Observable class not compatible with Codable?
So any time I create a class that's both @Observable and Codable, e.g. @Observable class GameLocationManager : Codable { I get a warning in the macro expansion code: @ObservationIgnored private let _$observationRegistrar = Observation.ObservationRegistrar() Immutable property will not be decoded because it is declared with an initial value which cannot be overwritten. I've been ignoring them for now, but there are at least a half a dozen of them now in my (relatively small) codebase, and I'd like to find a solution (ideally one that doesn't require me to write init(decoder:) for every @Observable class in my project...), especially since I'm not sure what the actual consequences of ignoring this might be.
2
0
904
Jul ’24
Cannot Get ScrollViewReader to scroll reliably across platforms.
I've searched this this all over this forum and the web, and I keep seeing "solutions" that don't actually seem to solve my (simple, I think) case. I'm trying to build a scrolling "conversation" view (think iMessage) where as each new line is added at the bottom, the view scrolls to completely display it. Here's what I've got: struct ConversationView: View { @Environment(GameModel.self) private var gameModel @State var shownSteps: Int = 0 var body: some View { VStack { HStack { Spacer() Button { //...random button actions... } label: { Text("Skip Conversation") } }.padding(5) ScrollViewReader { proxy in List { ForEach (gameModel.conversationPoints) {cp in let ind = gameModel.conversationPoints.firstIndex(of: cp)! if (ind <= shownSteps) { ConversationLineView(step: ind, shownSteps: $shownSteps).border(Color.blue) .id(cp.id) } } }.onChange(of: shownSteps) { a, b in proxy.scrollTo(gameModel.conversationPoints[shownSteps].id, anchor: .bottom)} } } } } Basically "shownSteps" is how many entries there are visible at the moment (out of a pre-populated list of "conversation points"), and every time it changes, I want to scroll to that entry. This looks to me identical to several "working" examples I've found online, and it's obviously close, because it works in some places, but not others: macOS: Code works fine in Sequioa betas, but doesn't scroll (new messages just show up below the bottom of the scroll region) at all in Sonoma. iPadOS: The opposite: Works in 17, doesn't work in 18. (My app doesn't run on phone, but I assume the same for iOS) visionOS: Works in 2.0 betas, haven't checked 1.2. Any ideas?
Topic: UI Frameworks SubTopic: SwiftUI
5
0
997
Jul ’24
Did RealityView syntax change?
All of a sudden (like when XCode 15.2 left beta yesterday?) I can't build attachments into my RealityView: var body: some View { RealityView { content, attachments in // stuff } attachments: { // stuff } Produces "No exact matches in call to initializer" on the declaration line (RealityView { content, attachments in). So far as I can tell, this is identical to the sample code provided at the WWDC session, but I've been fussing with various syntaxes for an hour now and I can't figure out what the heck it wants.
2
0
899
Jan ’24