Post

Replies

Boosts

Views

Activity

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 @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 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 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 "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 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 .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 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 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 Remove Back Button Text for Entire App
You can’t do this yet in SwiftUI. In UIKit you would do something like this:Swift navigationItem.backButtonDisplayMode = .minimal so you could try to implement this into your views. You could also use UIViewControllerRepresentable to create your own UINavigationController and embed your views inside that. To change the back button you would do this:Swift navigationBar.topItem?.backButtonDisplayMode = .minimal Hopefully a lot of navigation features from UIKit come to SwiftUI this year.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to Changing background color from user input
You can do something like this: struct ContentView: View { @State private var backgroundColour: Color = .red var body: some View { ZStack { backgroundColour .ignoresSafeArea() Button("Change background colour") { backgroundColour = .blue } } } } You need to create an @State property to hold the current background colour and update it when the button is tapped.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to ignoreSafeArea on some devices
There is no way to get the device model, only the device type (iPhone, iPad, etc.) This post - https://stackoverflow.com/questions/26028918/how-to-determine-the-current-iphone-device-model, however, extends UIDevice to accommodate all the device types.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21