Post

Replies

Boosts

Views

Activity

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 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 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 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