Post

Replies

Boosts

Views

Activity

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 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 Hide separators in List (SwiftUI)
I think the best way I’ve found is this: ScrollView { LazyVStack(pinnedViews: .sectionHeaders) { // list content goes here } } The optional pinnedViews parameter can be used to pin the section headers (or footers, or both) to the top of the list - like the PlainListStyle. You will need to, of course, have Sections in the list. You might need to apply some frame modifiers to the content and some padding to inset it from the edge of the screen.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to Alerts and Action Sheets not using SwiftUI accent color
I am having this same problem to. The accent colour does affect alerts and action sheets but for some reason they don't show up straight away. When the alert/action sheet is presented try holding on one of the buttons and then dragging your finger off of it, so that you're not pressing the button but just interacting with it - sort of making it active. You will see that the button does take on the accent colour. I think this is just a bug where these controls show the blue tint colour by default without consulting the asset catalog for the correct accent colour (if there is one).
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to PreviewUpdateTimedOutError: Updating took more than 5 seconds
Sometimes I get this error in the Xcode preview canvas and all I had to do was press Try Again and it would rebuild without an error. If there is a subsequent error, it is likely because of the code or that preview is actually crashing due to an error that you would need to work out. I wouldn’t worry about this particular error, though Apple should fix this soon or at least provide a more descriptive reason as to why the preview crashes. There are some people who have posted about this error online who have found a fix for it, so I would recommend you have a look at these. The bottom line is that this is not the error you need to be concerned about; there is likely another error (in the code or elsewhere) that is your main problem.
Feb ’21
Reply to Right sidebar
In SwiftUI, not yet. You could implement your own custom trailing sidebar in SwiftUI or integrate some UIKit but that would be quite difficult. You’ll have to wait for WWDC21 for any improvements to SwiftUI.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21
Reply to It is possible to set a background image behind a FORM in swiftUI?
You can add this to your view struct:.onAppear { UITableView.appearance().backgroundView = UIImageView(image: UIImage(named: "imageName")) } It uses a bit of UIKit to change the background view behind the form, not including the rows. The image being passed in to the UIImageView needs to be a UIImage. Take a look at the documentation - https://developer.apple.com/documentation/uikit/uiimage if you need more information on how to configure one.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’21