Post

Replies

Boosts

Views

Activity

Reply to SwiftUI & Layout API. Extra trailing closure passed in call
The method behind the trailing closure accepts a closure as a parameter that can only return a single view. func callAsFunction<V>(_ content: () -> V) -> some View where V : View Since its not marked with @ViewBuilder, like most of the other SwiftUI view closures, you can only return one view. So your view will have to be declared like this if you want multiple views: MyStairsStack() { Group { Text("Hello, World!") Text("Hello, World!") Text("Hello, World!") } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’22
Reply to EditView Reference is Broken
The problem is that you have attached the .sheet modifier to the view inside of the ForEach, which means that every view has that sheet that presents its own version of EditView. Also the .contextMenu modifier should be attached to each view in the ForEach so it has access to the object that has been selected. I have changed your code so that this should (theoretically) work. // When the button in the context menu is tapped, the index of the corresponding object is stored in the selectedObjectIndex variable. // This triggers the sheet to appear with an unwrapped selectedObjectIndex which is used to get the object that is passed to the EditView. struct ContentView: View { @State private var objectList = [Object(title: "First", string: "Editor"), Object(title: "Second", string: "Addition"), Object(title: "Third", string: "Twelve")]     @State private var selectedObjectIndex: Int?         var body: some View {         NavigationView{             List{                 Section("Objects"){                     ForEach(objectList) { object in                         NavigationLink(destination: ObjectView(obj: object)){                             Text(object.title)                         } .contextMenu{ // move inside ForEach                     Button(action: {                         self.selectedObjectIndex = objectList.firstIndex(of: object)                     }){                         Text("Edit Item")                     }                 }                     }                 } .sheet(item: $selectedObjectIndex) { index in // move outside ForEach and use item initialiser                 EditView(obj: objectList[index])                 }             }             .listStyle(InsetGroupedListStyle())             .cornerRadius(10)             .navigationBarTitle("My List")         }     } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’22
Reply to SwiftUI Textfield Alert for macOS
macOS 13 Beta Release Notes SwiftUI New Features • You can now place a TextField in an Alert by using alert modifiers that accept a ViewBuilder . (64819930) I haven't tested it yet as I'm still using Monterey, but it should work according to the release notes. If you are looking for macOS 12 and earlier solutions, you can do something like this: Button("Tap Me") { let alert = NSAlert() alert.messageText = "Alert with text field"     alert.addButton(withTitle: "OK") // You can customise this text field, this is just an example.     alert.accessoryView = NSTextField(string: "Text")                  alert.runModal() }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’22
Reply to NavigationStack Being Automatically dismissed
One thing that may be causing this is that fact that the FruitDetailView contains a NavigationStack, when it's parent, ContentView, already wraps it in one. If removing the inner NavigationStack doesn't work, then maybe it's a bug. I'm not sure how related this is but from the release notes: A view-based NavigationLink in a List fails to update the visible selection of the list. (92193873)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’22
Reply to MyEqualWidthHStack WWDC example not working for me
A lot of people have been experiencing different behaviours with the new Layout API and they each have different solutions. Resolved in iOS & iPadOS 16 beta 2, passing multiple children to a custom Layout now compiles. This means that a Group or another container is no longer required. The other issue arises from how the layout views are created under the hood. Two solutions for this are either wrapping the layout name in parentheses: (CustomLayout()) { Text("Hello, World") Text("Hello, World") } or creating an initialiser for the custom Layout that accepts no parameters: init() {} ... CustomLayout { Text("Hello, World") Text("Hello, World") }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’22
Reply to NavigationSplitView with Custom Views as Labels
I'm not really sure what you're asking. If you want to know how to show a different view based on the current selection then you can see the ammended code below: @State var selectedNavigationCategory: NavigationCategory? let navigationCategories = [     NavigationCategory(id: UUID(), name: "One", sfSymbol: "circle"),     NavigationCategory(id: UUID(), name: "Two", sfSymbol: "circle.fill") ] var body: some View {     NavigationSplitView {         List(navigationCategories, selection: $selectedNavigationCategory) { category in             NavigationLink(value: category) {                 Label(category.name, systemImage: category.sfSymbol)             }         }     } detail: {         ZStack { // workaround in beta 2             if let selectedNavigationCategory {                 // Something has been selected // Show the corresponding view for this                 Text("You selected \(selectedNavigationCategory.name)")             } else {                 // Nothing is selected // Placeholder text                 Text("Select something")             }         }     } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’22
Reply to Unable to change Toggles
Move the @State property wrapper from the selected variable in the mySession struct to the sessions array. You can then pass a binding directly into the Toggle, like this: ForEach($sessions) { $session in Toggle(isOn: $session.selected) { Text(session.name) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’22
Reply to I have problem with this code. I try to append it but i get some error so please help
The variable Produkt is of type Array<ScannProdukt>. In the addRow method, you are appending the value stored in ScannedCode to this array which is of type String. The error comes from the fact that the array expects a value of type ScannProdukt to be appended, but instead got a value of type String. You would need to use the ScannedCode property to create a ScannProdukt that can be appended to the Produkt array.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’22
Reply to Safe Area Insets
I'm assuming you're using SwiftUI since that's what you tagged the question with. If so, your views should automatically be placed to fit in the safe area. Using the ignoresSafeArea(_:edges:) modifier, well, ignores the safe area, so expanding to fill the whole screen. If you want your views to fill the entire space inside of the safe area, you can use a frame modifier like this: .frame(maxWidth: .infinity, maxHeight: .infinity) If you explicitly want the top and bottom safe area inset values, then you can wrap your view in a GeometryReader and access the values like this: GeometryReader { geoProxy in let topInset = geoProxy.safeAreaInsets.top let bottomInset = geoProxy.safeAreaInsets.bottom ... }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’22
Reply to SwiftUI 4: Set list background color
New in iOS 16 beta 3, two new modifiers! You can use them to set a background colour for a list: List { ... } .scrollContentBackground(Color.red) // using .red results in an error or set a custom background view: List { ... } .scrollContentBackground(.hidden) .background {     Image(systemName: "list.bullet")         .resizable()         .scaledToFit()         .padding() }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’22
Reply to Can't change List background color in iOS 16
New in iOS 16 beta 3, two new modifiers! You can use them to set a background colour for a list: List { ... } .scrollContentBackground(Color.red) // using .red results in an error or set a custom background view: List { ... } .scrollContentBackground(.hidden) .background {     Image(systemName: "list.bullet")         .resizable()         .scaledToFit()         .padding() }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’22