Post

Replies

Boosts

Views

Activity

Reply to How to put checkmark when Menu item selected SwiftUI
You would use a Picker. For example: Picker("Menu picker", selection: $selection) { ForEach(1..<5, id: \.self) { number in Label("\(number)", systemImage: "\(number).circle") } } .pickerStyle(MenuPickerStyle()) // makes the picker appear as a menu If you want the picker to be embedded inside the menu do this: Menu("Picker inside menu") { Text("Menu item 1") Text("Menu item 2") Divider() Picker(...) { ... } .pickerStyle(MenuPickerStyle()) // apply this if you want the picker to show as a sub-menu }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to How to add a label to a SwiftUI Textfield in macOS
I don’t know if this is what you want, but I’ve made a custom alignment guide that will align a label and a control with the centre of the view. This alignment style is used pretty much everywhere on macOS: for example, the General page in System Preferences. Swift extension HorizontalAlignment { private struct CentreLine: AlignmentID { static func defaultValue(in context: ViewDimensions) - CGFloat { context[HorizontalAlignment.center] } } static let centreLine = Self(CentreLine.self) } I also made a custom Row View that handles the alignment of the label and control for you: Swift struct RowLabel: View, Control: View { private let label: Label private let control: Control init(label: Label, @ViewBuilder control: () - Control) { self.label = label self.control = control() } init(@ViewBuilder control: () - Control) where Label == EmptyView { self.init(label: EmptyView(), control: control) } var body: some View { HStack { label.alignmentGuide(.centreLine) { $0[.trailing] } control.alignmentGuide(.centreLine) { $0[.leading] } } } } This can then be used like this: Swift // need to have the alignment parameter for it to work VStack(alignment: .centreLine) { // with label Row(label: Text("Username:")) { TextField("Enter username", text: $username) } // without label but still aligned correctly Row { Toggle("Show password", isOn: $showingPassword) } }
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
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 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 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