Post

Replies

Boosts

Views

Activity

PencilKitCanvas cannot becomeFirstResponder in SwiftUI
I have done the same thing in SwiftUI using UIViewRepresentable, but toolPicker doesn't show so I checked isFirstResponder property and I found that it was still false after I called canvas.becomeFirstResponder(). Check this out: struct NoteCanvasView: UIViewRepresentable {     func makeUIView(context: Context) -> PKCanvasView {         let canvas = PKCanvasView()         canvas.drawingPolicy = .anyInput         canvas.delegate = context.coordinator.self                  let toolPicker = PKToolPicker()         toolPicker.setVisible(true, forFirstResponder: canvas)         toolPicker.addObserver(canvas)         print(canvas.canBecomeFirstResponder)         canvas.becomeFirstResponder()         print(canvas.isFirstResponder)         return canvas     }          func updateUIView(_ canvas: PKCanvasView, context: Context) {         canvas.becomeFirstResponder()     }          func makeCoordinator() -> Coordinator {         Coordinator(self)     }          class Coordinator: NSObject {         var parent: NoteCanvasView         init(_ parent: NoteCanvasView) {             self.parent = parent         }     } } I found canvas.canBecomeFirstResponder returns true and canvas.isFirstResponder always returns false. Is this a bug in current version of SwiftUI??
0
0
737
Aug ’21
nsPredicate disappeared when updated
I am working on a SwiftUI project and in a subview I use @FetchRequest to fetch data from CoreData. I have a menu to let user select which data to fetch, and by default I want to fetch all datas. The problem is when user open the menu and select a category then it refetch the data, it's correct and when user close the menu, the nsPredicate that the user have given will disappear and switch to the default predicate. I tried to write the same pattern code on 'EarthQuake' sample app, it has the same problem. And here is the code: List(selection: $selection) {                 ListView(search: $searchText)             }             .background(toggle ? Color.red.opacity(0.01) : nil)             .toolbar {                 ToolbarItem(placement: .bottomBar) {                     Button {                         toggle.toggle()                     } label: {                         Text("Toggle")                     }                 }             }             .searchable(text: $searchText) ListView: struct ListView: View {     @FetchRequest(sortDescriptors: [SortDescriptor(\.time, order: .reverse)]) // predicate: nil     private var quakes: FetchedResults<Quake>     @Binding var search: String     var body: some View {         ForEach(quakes, id: \.code) { quake in             NavigationLink(destination: QuakeDetail(quake: quake)) {                 QuakeRow(quake: quake)             }         }         .onChange(of: search) { newValue in             quakes.nsPredicate = newValue.isEmpty ? nil : NSPredicate(format: "place CONTAINS %@", newValue)         }     } } I can filter data by typing in the search field but when I click on Toggle Button, it may refresh the ParentView which cause the @fetchRequest to update and nsPredicate return to nil. Is this a bug? Or maybe my understand was wrong. And is there any good suggestions?
1
0
1.3k
Nov ’21
Back supported Navigation-View
Is there any convenient way to back deploy the NavigationStack or NavigationSplitView?? The real problem is if I want to back support iOS 15 or 14, I must conditionally switch between NavigationView and NavigationStack / NavigationSplitView. Here is how I did for NavigationStack, but I have no idea how to deal with NavigationSplitView import SwiftUI struct NavigationStack<Content: View>: View {     var content: () -> Content     var body: some View {         if #available(iOS 16.0, macOS 13.0, *) {             SwiftUI.NavigationStack {                 content()             }         } else {             NavigationView {                 content()             }.navigationViewStyle(.stack)         }     } } Will the new NavigationStack and NavigationSplitView back support old devices? I think these behaviors in previous OS is not new features.
1
0
1.6k
Jul ’22
PKCanvasView size error
Here is the implementation. import SwiftUI import PencilKit struct DrawingCanvas_bug: UIViewRepresentable { typealias UIViewType = PKCanvasView private let toolPicker = PKToolPicker() @Binding var drawing: PKDrawing var isOpaque: Bool = true var drawingDidChange: ((PKDrawing) -> Void)? func makeUIView(context: Context) -> PKCanvasView { let canvasView = PKCanvasView(frame: .zero) canvasView.drawing = drawing canvasView.delegate = context.coordinator canvasView.backgroundColor = .clear canvasView.isOpaque = isOpaque canvasView.alwaysBounceVertical = true // The size of the canvas is Zero, so I cannot update the ZoomScale now. // context.coordinator.updateZoomScale(for: canvasView) toolPicker.setVisible(true, forFirstResponder: canvasView) toolPicker.addObserver(canvasView) canvasView.becomeFirstResponder() return canvasView } func updateUIView(_ canvasView: PKCanvasView, context: Context) { DispatchQueue.main.async { // We can get the correct ZoomScale and the correct ContentSize, but part of the drawing is not visible. // Using the select tool can select them. context.coordinator.updateZoomScale(for: canvasView) } } func makeCoordinator() -> Coordinator { Coordinator(self) } static func dismantleUIView(_ canvasView: PKCanvasView, coordinator: Coordinator) { canvasView.resignFirstResponder() } } extension DrawingCanvas_bug { class Coordinator: NSObject, PKCanvasViewDelegate { var host: DrawingCanvas_bug init(_ host: DrawingCanvas_bug) { self.host = host } func canvasViewDrawingDidChange(_ canvasView: PKCanvasView) { host.drawing = canvasView.drawing if let action = host.drawingDidChange { action(canvasView.drawing) } updateContentSizeForDrawing(for: canvasView) } func updateZoomScale(for canvasView: PKCanvasView) { let canvasScale = canvasView.bounds.width / 768 canvasView.minimumZoomScale = canvasScale canvasView.maximumZoomScale = canvasScale canvasView.zoomScale = canvasScale updateContentSizeForDrawing(for: canvasView) } func updateContentSizeForDrawing(for canvasView: PKCanvasView) { let drawing = canvasView.drawing let contentHeight: CGFloat if !drawing.bounds.isNull { contentHeight = max(canvasView.bounds.height, (drawing.bounds.maxY + 500) * canvasView.zoomScale) } else { contentHeight = canvasView.bounds.height } canvasView.contentSize = CGSize(width: 768 * canvasView.zoomScale, height: contentHeight) } } } And here is how I use: NavigationSplitView(columnVisibility: .constant(.doubleColumn)) {     List(selection: $selection) {         ForEach(drawingModel.drawings, id: \.uuidString) {             DrawingRow(drawingData: $0)         }     } } detail: {     DrawingView() } .navigationSplitViewStyle(.balanced) // <- If I use automatic style, PKCanvasView's size is correct
1
0
1.6k
Aug ’22
SwiftUI Support
How to add a custom button to the Edit Menu on both iOS and iPadOS natively using SwiftUI. I have seen many ways to implement custom button using UIKit but wondering how to use SwiftUI to achieve the same thing. I have never seen any modifiers or Menus about this. I guest there is no way to do that. Any idea about this? or is it a future update for SwiftUI ??
4
0
2.3k
Aug ’22
App Terminates when using Window and MenuBarExtra in SwiftUI
If you configure a Window and a MenuBarExtra in your app, try this: Open your app and press Command+H to hide it. Place an iPad or another Mac and connect two devices via Universal Control, and click on the other device to make your mac's menu bar inactive. Move your cursor back to your Mac(first device), and click on the whitespace of the Menu Bar. Your App is terminated unexpectedly. This behavior will only appear on those apps using Window, so to solve this issue, we may need to switch Window to WindowGroup which is not what we want. I reappear this issue on Apple's ScreenCaptureKit Sample Code. This is a SwiftUI Scene BUG, please fix this. @main struct CaptureSampleApp: App { var body: some Scene { Window("ID", id: "ID") { ContentView() .frame(minWidth: 960, minHeight: 724) .background(.black) } MenuBarExtra( "App Menu Bar Extra", systemImage: "star" ) { Text("Hello") } } } Filed a feedback: FB11447959.
0
0
1.3k
Sep ’22
App Intents not appear in Shortcuts App
App shortcuts can only be added to Shortcut Action List, but not a separate App Shortcuts appears at the bottom of the Shortcuts app. Now it only has the default Voice Memo App. It successfully appeared in beta 2/3, but I'm not sure if it appeared in (beta 3 update). But in beta 4, it disappeared. I have no idea how to make it visible again!!!
3
1
3.3k
Sep ’22
ShareLink from Menu cannot popup
Menu {      ShareLink(...) ShareLink(...)      ShareLink(...) } label: {   Label("Share", systemImage: "square.and.arrow.up") } Here, I have different share options to choose from, and if I tap one of these share link to share sheet doesn't pop up. Is there any workaround to this issue?
3
2
1.8k
Oct ’22