Post

Replies

Boosts

Views

Activity

Reply to privacySensitive() doesn't work
Applying the .privacySensitive() modifier won’t actually redact the text, it just tells SwiftUI that the text is privacy sensitive. To actually redact the text, apply the .redacted(reason: .privacy) somewhere higher up in the view hierarchy. For example, in your case, you could add the .redacted(reason:) modifier to you’re VStack if you wanted to preview what it looks like. Check out these links about more on how to use this modifier: https://developer.apple.com/documentation/swiftui/text/privacysensitive(_:) https://developer.apple.com/documentation/swiftui/redactionreasons/privacy https://www.hackingwithswift.com/quick-start/swiftui/how-to-mark-content-as-private-using-privacysensitive
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21
Reply to SwiftUI scroll view page indicator color
Do you mean a TabView with the tab view style of .page (PageTabViewStyle)? If so, it's currently not possible in SwiftUI (as of iOS 15 beta 5). You can, however, reach down into UIKit and modify some of its appearance API. Here's some examples of its usage: // All page dots have a colour of red UIPageControl.appearance().pageIndicatorTintColor = .systemRed // Only the current page's dot is green UIPageControl.appearance().currentPageIndicatorTintColor = .systemGreen // All page dots have a colour of indigo but the current page's dot is blue UIPageControl.appearance().currentPageIndicatorTintColor = .systemBlue UIPageControl.appearance().pageIndicatorTintColor = .systemIndigo Place this in the initialiser of your view. struct SomeView: View { init() { // modify appearance } var body: some View { … } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21
Reply to How to perform extract subview operation?
This isn’t a new thing in SwiftUI. It’s more to do with Swift and structs. The error message is saying that it can’t find the variable room inside itself (as a property of the struct). You need to add this property called room to your ExtractedView and pass this in from your ContentView, like this: struct ExtractedView: View { let room: Room // now it knows what 'room' is   var body: some View { … } } In ContentView: List(rooms) { room in ExtractedView(room: room) // give the view the room it needs to show }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21
Reply to Constraints misplaced
You need to add two alignment constraints to place the views in the centre of the screen. If you want to, you can place the two views inside a UIStackView to make positioning easier. To do this select the label and button, click the Embed In button in the bottom right of the screen (fifth from the left) and choose Stack View. You can modify how the stack view positions its containing views. To align this stack view, select it and click the Align button in the bottom right of the screen (second from the left) and add Horizontally in Container and Vertically in Container constraints. This should position the label and button in the centre of the device. If you don't want the label and button in a UIStackView you can always add constraints relative to each other. Hope this helps.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21
Reply to How to set foregroundColor/fontWeight for a ToolBarItem?
Resolved in iOS & iPadOS 15 beta 2 Buttons in toolbar items now respect custom styles or customizations to their labels. This means that you can customise the appearance of toolbar buttons however you like. If you wanted the destructive red style button, you can specify a role for the button, like this: Button(role: .destructive) { … } label: { … } NOTE: This will only work with iOS 15, macOS 12 and similar.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Reply to List-like Selection for custom containers
From the documentation for this modifier: Use an item-based context menu with a container that supports selection, such as a List or Table. I don't think it is possible as there is no current API for selection in custom views as this is what the context menu is looking for.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’22
Reply to Time Wheel Picker
I asked a similar question a while back and it doesn't look like anything has changed. You definitely can't do this in SwiftUI, only with UIKit. You will have to manually position the hour and min text labels and set the width of the picker so that the scrolling numbers line up properly.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’22
Reply to Canceling an Edit: When to alert?
I believe NSManagedObject has a hasChanges property so you can check that boolean value, however this requires you to update the object directly whenever the user changes something. The other option would be doing what @Claude31 suggested, or manually checking each property for a change between the current stored value and the one the user is modifying.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’22
Reply to Canceling an Edit: When to alert?
Here's my attempt at using the hasChanges property. It sort of combines the two approaches. See what you think – I'm not sure how well it works though. There is an entity called Item with three attributes: name: String, detail: String, amount: Int16 (for reproducibility). I used the template PersistenceController struct to inject the managedObjectContext into the views. struct ContentView: View {     @Environment(\.managedObjectContext) private var viewContext     @FetchRequest(sortDescriptors: []) private var items: FetchedResults<Item>     @State private var selectedItem: Item?     var body: some View {         NavigationStack {             List(items) { item in                 Button {                     selectedItem = item                 } label: {                     LabeledContent(item.title ?? "No name", value: item.amount, format: .number)                 }             }             .sheet(item: $selectedItem, content: EditView.init)             .toolbar {                 Button(action: addItem) {                     Label("Add Item", systemImage: "plus")                 }             }         }     }     private func addItem() {         withAnimation {             let newItem = Item(context: viewContext)             newItem.title = "New Item"             do {                 try viewContext.save()             } catch {                 fatalError("Failed to save: \(error.localizedDescription)")             }         }     } } struct EditView: View {     @Environment(\.dismiss) private var dismiss     @State private var showingCancelAlert = false     @State private var title: String     @State private var detail: String     @State private var amount: Int     let item: Item     init(item: Item) {         self.item = item         _title = State(wrappedValue: item.title ?? "")         _detail = State(wrappedValue: item.detail ?? "")         _amount = State(wrappedValue: Int(item.amount))     }     var body: some View {         NavigationStack {             Form {                 Section {                     TextField("Title", text: $title)                         .onSubmit {                             item.title = title                         }                     TextField("Description", text: $detail)                         .onSubmit {                             item.detail = detail                         }                 }                 Stepper("Amount: \(amount)", value: $amount) { _ in                     item.amount = Int16(amount)                 }             }             .navigationTitle("Edit Item")             .navigationBarTitleDisplayMode(.inline)             .toolbar {                 ToolbarItem(placement: .cancellationAction) {                     Button("Cancel", role: .cancel) {                         if item.hasChanges {                             showingCancelAlert = true                         } else {                             dismiss()                         }                     }                     .confirmationDialog("You have unsaved changes", isPresented: $showingCancelAlert) {                         Button("Discard Changes", role: .destructive, action: dismiss.callAsFunction)                     }                 }                 ToolbarItem(placement: .confirmationAction) {                     Button("Done", action: dismiss.callAsFunction)                         .disabled(!item.hasChanges)                 }             }         }         .interactiveDismissDisabled()     } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’22
Reply to placeholderText Color
If you want the placeholderText property for SwiftUI's Color, you can access it like this: Color(uiColor: .placeholderText) You can also wrap this inside of an extension on Color for simpler code.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’22