Post

Replies

Boosts

Views

Activity

MagnificationGesture needs location?
I tried to replace UIKit's UIPinchGestureRecognizer with SwiftUI's MagnificationGesture, but it doesn't seem possible in my case. I use the location(in: UIView) function from the former, which allows me to zoom in on that point, specifically. It's a better experience when zooming in on an image. Can I get that info from the MaginificationGesture? In the example code I see only the CGFloat for the amount of magnification.
0
1
545
Oct ’21
SwiftUI sheet size control on iPad
I want to show a custom confirmation dialog on my iPad. It pops up looking utterly ridiculous because the size is way bigger than it needs - inches of padding on all sides. Is there still no way to control a sheet size on iPad? Example code: struct ContentView: View {     @State var showSheet = false     var body: some View {         Button("Show sheet") {             showSheet = true         }.sheet(isPresented: $showSheet) {             VStack(spacing: 0) {                 Text("Title")                 Divider()                 Text("Line 1")                 Text("Line 2. Blah blah blah blah.")                 Divider()                 HStack {                     Button("Cancel") { showSheet = false }                     Divider()                     Button("OK") { showSheet = false }                 }.fixedSize(horizontal: false, vertical: true)             }             .frame(minWidth: 0, minHeight: 0)             .fixedSize()         }     } } The result:
1
0
626
Sep ’21
Wrapped UIActivityViewController is blank on first showing
Here's the entire app below. On iOS 15, it pops up blank. If you dismiss it and click "Share" again, then it looks right. Is this a bug, or is the code wrong? struct ContentView: View {     @State private var showShare = false     @State private var shareItems: [Any] = []     var body: some View {         VStack {             Text("Test ActivityViewController")             Button("Share") {                 share()             }.sheet(isPresented: $showShare) {                 print("dismissed")             } content: {                 ActivityViewController(activityItems: shareItems)             }         }     }     func share() {         shareItems = ["test"]         showShare = true     } } struct ActivityViewController: UIViewControllerRepresentable {     let activityItems: [Any]     func makeUIViewController(context: Context) -> UIActivityViewController {         let c = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)         return c     }     func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {} }
0
1
727
Sep ’21
Can't interpolate a Bool in SwiftUI Text?
Simple code like this gives an error. struct MyView: View {     @State private var test: Bool = false     var body: some View {           Text("Hello. \(test)") The error: Instance method 'appendInterpolation(_:formatter:)' requires that 'Bool' inherit from 'NSObject' What is going on?
2
1
3.9k
Sep ’21
Too many empty "required" UIView.init(coder:) methods
Hi,I have a lot of UIViews where the compiler forces me to add an init(coder:) initializer, like this:class FooView : UIView /* or a UIView subclass */ { ... required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } ... }It claims it's required but my program runs fine without it. I do not create the views from an archive.This makes me wonder if something is wrong here with the design of the library, or the concept of a 'required' initializer. What do people think? Does it make sense or is this a wart? If so, can it be fixed?Rob
Topic: UI Frameworks SubTopic: UIKit Tags:
5
1
2.8k
Sep ’21
Can't use protocols with SwiftUI models?
I've been using protocols to help model a hierarchy of different object types. As I try to convert my app to use SwiftUI, I'm finding that protocols don't work with the ObservableObject that you need for SwiftUI models. I wonder if there are some techniques to get around this, or if people are just giving up on "protocol oriented programming" when describing their SwftUI models? There is example code below. The main problem is that it seems impossible to have a View that with an model of protocol `P1` that conditionally shows a subview with more properties if that model also conforms to protocol `P2`.For example, I'm creating a drawing/painting app, so I have "Markers" which draw on the canvas. Markers have different properties like color, size, shape, ability to work with gradients. Modeling these properties with protocols seems ideal. You're not restricted with a single inheritance class hierarchy. But there is no way to test and down-cast the protocol...protocol Marker : ObservableObject { var name: String { get set } } protocol MarkerWithSize: Marker { var size: Float { get set } } class BasicMarker : MarkerWithSize { init() {} @Published var name: String = "test" @Published var size: Float = 1.0 } struct ContentView<MT: Marker>: View { @ObservedObject var marker: MT var body: some View { VStack { Text("Marker name: \(marker.name)") if marker is MarkerWithSize { // This next line fails // Error: Protocol type 'MarkerWithSize' cannot conform to 'MarkerWithSize' // because only concrete types can conform to protocols MarkerWithSizeSection(marker: marker as! MarkerWithSize) } } } } struct MarkerWithSizeSection<M: MarkerWithSize>: View { @ObservedObject var marker: M var body: some View { VStack { Text("Size: \(marker.size)") Slider(value: $marker.size, in: 1...50) } } }Thoughts?
3
1
9.4k
Sep ’21
Popping back multiple levels?
I have a UI where you can navigate/push views like this: Root view > List of things > View thing > Edit thingThe "Edit thing" view can also delete it. After a delete, I want it to pop back to the "List of things". Best I've got now is to call `presentationMode.wrappedValue.dismiss()` on the "Edit thing" view, and then again in the "View thing" view, but that time inside DispatchQueue.main.async { }. It works but the double animation is kind of clunky.Is there a better way?
3
0
7.5k
Sep ’21
Upgrading app's UIDocument format from Data to FileWrapper?
Hi,I had a document-based iOS app working, but want to change it so it saves to a package. Seems like it's better when big chunks of a file may not be changing. In Xcode, under the Target > Info > Exported UTI > Conforms To, I had: "public.data, public.content". If I change that to "com.apple.package", then I can't open my old files to upgrade them. But if I *add* "com.apple.package", then the app opens both kinds as desired. I wonder if having it conform to all three of those types is going to cause other problems.Rob
Topic: UI Frameworks SubTopic: UIKit Tags:
3
0
1.5k
Jul ’21
Can't run unit tests with Xcode 12.5. Code signing?
After updating to 12.5, I now get an error trying to run my unit tests. This is for a macOS app. dyld: warning: could not load inserted library '/[...]/DerivedData/[...]/Contents/Frameworks/libXCTestBundleInject.dylib' into hardened process because no suitable image found.  Did find: /[...]/libXCTestBundleInject.dylib: code signature in (/[...]/libXCTestBundleInject.dylib) not valid for use in process using Library Validation: mapped file has no Team ID and is not a platform binary (signed with custom identity or adhoc?) Anyone else face and fix this, or understand what it is? I changed the "Signing Certificate" in my test target to "Sign to Run Locally" (from "Development") to see if that would help. It didn't fix it.
3
0
4.6k
Jun ’21
Logger and signposts?
I've been using the os.Logger API, but it looks like I need to create an OSLog as well if I want to use the signpost API for tracking performance. Is that true, or is there some way get an underlying OSLog from a Logger? Then I could write something like: swift extension os.Logger { func signpost(...) { os_signpost(.begin, ... self.osLog, ...) } }
1
0
669
Apr ’21
How to respond to builtin main menu items like "Select All" in SwiftUI?
I know how to add items to the main menu. But what if I want to connect a handler to one that is already there by default? (For example "Select All"). WindowGroup { ContentView() }.commands {     CommandGroup(after: CommandGroupPlacement.pasteboard) {   Button("Select All") { selectAll() } } That adds a second "Select All" menu item. If I use CommandGroup(replacing: ...) then it replaces others, not just the "Select All"
0
1
597
Apr ’21
How to set cursor to show drag possibility - onHover fails
I have a View with a DragGesture to resize something, so I tried using onHover to set the macOS cursor to the right resize icon. It doesn't work 100%, because the drag will reset the cursor. Then I have to move the pointer out of the view and back in, to re-trigger the onHover and get the resize cursor again. swift private struct HeaderEdgeDragArea: View {     var drag: some Gesture {         DragGesture(minimumDistance: 0, coordinateSpace: .global)             .onChanged { value in ...             }             .onEnded { _ in ...             }     }     var body: some View {         Color.gray.zIndex(2.0).frame(width: 1.0)             .overlay(                 Rectangle().frame(width: 8)                     .foregroundColor(.clear)                     .contentShape(Rectangle())                     .border(Color.red, width: 0.5)                     .onHover { hovering in                         print("hovering: \(hovering)")                         if hovering {                             NSCursor.resizeLeftRight.push()                         } else {                             NSCursor.pop()                         }                     }                     .gesture(drag)             )     } }
0
0
511
Apr ’21
SwiftUI bug: Alert shows twice?
I'm running this on macOS. I looks like a bug to me. If I activate the menu item and confirm the alert, the alert pops up again. It only double-shows once; then it behaves correctly. In my real app, it double-shows every time. If I uncomment that DispatchQueue.main.async, it "fixes" it. macOS 11.2.3, Xcode 12.4. swift @main struct DoubleAlertApp: App {     @State var showAlert: Bool = false     @StateObject var model: Model = .init()     var body: some Scene {         WindowGroup {             ContentView().environmentObject(model)                 .background(EmptyView()                     .alert(isPresented: $showAlert) {                          Alert(title: Text("Test"),                                message: Text("This will do something"),                                primaryButton: .cancel(),                                secondaryButton: .destructive(Text("Do it"), action: confirmedAction))                     })         }.commands {             CommandGroup(replacing: CommandGroupPlacement.newItem) {                 Button(action: testAlert) {                     Text("Test Alert")                 }.keyboardShortcut("t")             }         }     }     private func testAlert() {         showAlert = true     }     private func confirmedAction() {         print("confirmedAction")         // DispatchQueue.main.async {             self.model.change()         //}     } } class Model: ObservableObject {     init() {}     @Published var foo: Int = 0     func change() {         objectWillChange.send()         foo += 1     } } struct ContentView: View {     @EnvironmentObject var model: Model     var body: some View {         Text("Hello. \(model.foo)").padding()     } }
2
0
2.3k
Mar ’21
Differentiating between "return" and focus loss in TextField onCommit
I have a TextField in my toolbar that I use for a search function. I want to trigger the search by hitting the "return" key in this field. But if I do it in onCommit, the search also gets triggered when the user un-focuses the field. Is there a way to respond to just the "return" key? TextField("Search", text: $searchQuery) { editing in print("onEditingChanged \(editing)") } onCommit: { // Problem: this is triggered by both // 1. Return key // 2. Losing focus     searchModel.startSearch(query: searchQuery) }
7
0
2.4k
Mar ’21