Post

Replies

Boosts

Views

Activity

Reply to What is the difference between Form and VStack in SwiftUI?
You can try it for yourself. Run these two bits of code and you will see the difference: Form { Text("I’m in a Form") Button("Tap Me!") { print("Button tapped") } } VStack { Text("I’m in a VStack") Button("Tap Me!") { print("Button tapped") } } A Form places the views in a List with an InsetGroupedListStyle (iOS). It is just a container that is platform-adaptive that shows a form. A VStack just places the views vertically: above and below each other. Check the documentation for Form - https://developer.apple.com/documentation/swiftui/form and VStack - https://developer.apple.com/documentation/swiftui/vstack for more detail.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to PreviewUpdateTimedOutError: Updating took more than 5 seconds
On your point about live previewing on device, I am currently having issues with this (see my post - https://developer.apple.com/forums/thread/664227 about this). In theory, if Xcode lets you preview on device, you should be able to view what's in the preview canvas on your device and interact with it as normal - this includes using the camera. I'm not sure why you keep getting the error, so if you haven't found anything to solve this elsewhere you should probably file a feedback report and attach the diagnostics from Xcode linked to the error.
Feb ’21
Reply to .localizedDescription
Error still has the localizedDescription property. This hasn’t changed. NSError has multiple localized... properties, so maybe that’s what you meant, but this does include localizedDescription. What is the errorLabel type? This might give the reason as to why you don’t see localizedDescription in your auto-complete popup.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to working with fileImporter in a list
I have run this code and it works as expected. Tapping import shows the file importer and the other rows shows a sheet. You can refine the code and tailor it towards your app. Swift enum MenuOption: String, CaseIterable, Identifiable { case `import` case optionB case optionC case optionD case optionE var id: Int { hashValue } @ViewBuilder var destination: some View { switch self { case .optionB: OptionBView() ... default: EmptyView() } } } Swift struct SidebarMenuView: View { @State private var selection: MenuOption? @State private var showingImport = false var body: some View { List { ForEach(MenuOption.allCases) { option in Button(option.rawValue) { switch option { case .import: showingImport = true default: selection = option } } .sheet(item: $selection) { option in option.destination } .fileImporter(isPresented: $showingImport, allowedContentTypes: [...], allowsMultipleSelection: true) { result in if let urls = try? result.get() { print("Got urls") } } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to "Missing Arguement for parameter 'navBarHidden' in call" Error
happy requires that a BindingBool be passed to it. There are two ways that I suggest you achieve this: Firstly, you can use the constant - https://developer.apple.com/documentation/swiftui/binding/constant(_:%29 static method to quickly show the view in the preview canvas in its different states just by changing the boolean value. Swift struct Good_Previews: PreviewProvider { static var previews: some View { happy(navBarHidden: .constant(true)) } } This is probably the preferred way for previewing and testing. The other way is to just use a State property, changing that, and pass that binding to the view as normal. Swift struct Good_Previews: PreviewProvider { @State static private var navBarHidden = true static var previews: some View { happy(navBarHidden: $navBarHidden) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’21
Reply to Trouble with SwiftUI and SwiftPlaygrounds
If you haven’t already, click on the speedometer button in the bottom left of the canvas and turn off Enable Results. It says it may reduce performance and I have had this issue myself. When this is on, the labels to the right of lines of code appear giving you some information about what it is doing and how many times the line has been run. This is likely the cause of the errors Playgrounds is giving you because it is working harder to produce these results instead of focusing on displaying the results in the canvas. If this doesn’t work, check your code for any possible errors: typos, syntax, etc.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to passing nil to `perferredColorScheme` instead of light/dark
This has been resolved iOS 14.5 beta 2. You can view the release notes - https://developer.apple.com/documentation/ios-ipados-release-notes/ios-ipados-14_5-beta-release-notes for more information on new features and resolved issues. Setting .preferredColorScheme(nil) now correctly resets to the system’s preferred color scheme. (67000774) If you are not using the latest beta software or Xcode releases then you will have to wait for the final release of iOS 14.5 which should be available in a couple of weeks.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to @State not working on Swift Playgrounds
I have run your code and experienced the same problem. You can fix this by clicking on the speedometer button in the bottom left of the canvas and turning off Enable Results. The labels to the right of the lines of code will disappear but that is at the cost of improving performance. This is either a bug with Playgrounds or an intentional feature. My advice is that when using SwiftUI, turn off this setting so that Playgrounds doesn’t have to work any harder to give you the extra results and instead focuses on rendering your views in the canvas.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to Change ListStyle based on horizontalSizeClass
I think I have found a temporary solution to this, albeit not the best one. Swift struct AdaptiveListStyle: ViewModifier { @Environment(\.horizontalSizeClass) private var horizontalSizeClass @ViewBuilder func makeBody(content: Content) { if horizontalSizeClass == .compact { content.listStyle(GroupedListStyle()) } else { content.listStyle(InsetGroupedListStyle()) } } } This will only work for just GroupedListStyle and InsetGroupedListStyle (static). I couldn’t find a way to pass in ListStyles without Xcode throwing many errors about mismatching types and generics, so I left that alone. If anyone can fix this or provide a workaround to the original problem, it would be much appreciated.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to SwiftUI tutorial
You can use .filter(_:) to filter through an array providing a closure that should return true or false depending on whether the element should appear in the filtered array. For the landmarks example, you can think of it as filter through the landmarks array and if we are not showing favourites only or the current element is favourited then this element should be filtered out into the new array. That might sound a bit confusing so you can also take a look at the documentation - https://developer.apple.com/documentation/swift/sequence/3018365-filter for a better summary.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to In a SwiftUI three split navigation view on iPad, how to check if the primary is folded?
You can use the onAppear and onDisappear modifiers on the primary view in the NavigationView. For example: Swift NavigationView { Text("Primary") .onAppear { print("Unfolded") } .onDisappear { print("Folded") }         Text("Supplementary") Text("Secondary") } The primary view is folded by default, so you can toggle an @State boolean variable when the primary view appears (unfolds) and disappears (folds).
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21
Reply to In a SwiftUI three split navigation view on iPad, how to check if the primary is folded?
I think that might be the only quirk with that particular solution. I’m sure there’s a workaround to this, or just an entirely different solution, but I’m confident you can solve this - maybe another boolean that is true on app launch and then toggled to false afterwards. Hopefully, Apple can introduce better APIs for these features at WWDC21.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’21