TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How to have "double-tap to edit text" functionality in a NavigationView?

Forums > SwiftUI

I'm making a macOS app (also iPadOS, but macOS is the priority) in SwiftUI, and I'm trying to implement functionality similar to finder, where you can double-tap an item in order to change it's name, but I'm not sure how to do that!

This is my current layout (stuff not really relevant to the question has been cut out for brevity), I want to edit the names of the colorBooks by double-tapping on the entry in the navigation view, and getting a text field until it leaves focus or you press enter or whatever.

@AppStorage("colorBooks") var colorBooks: [String: URL] = [:]

var body: some View {
    NavigationView {
            VStack(alignment: .leading) {
                List {
                    Text("Color Books")
                        .font(.system(size: 12, weight: .bold, design: .default))
                        .foregroundColor(.gray)
                    ForEach(Array(colorBooks.keys).sorted(by: >), id: \.self) { key in
                        if let path = colorBooks[key], let colorBook = ColorBook(path) {
                            NavigationLink(destination: ColorBookView(colorBook)) {
                                Label(key, systemImage: "tag")
                                    .font(.system(size: 13, weight: .medium, design: .default))
                            }
                            .buttonStyle(.borderless)
                            .navigationTitle(key)
                            .contextMenu {
                                // ...
                            }
                        }
                    }
                }.listStyle(SidebarListStyle())
                // ...
            }
        }
        .navigationViewStyle(DoubleColumnNavigationViewStyle())
}

2      

I don't know this is possible with your approach. Because as soon as you tap the NavigationLink fires. But I could be wrong. But my guess is you can't use a NavigationLink here but you have to use an .onTapGesture for both where you have to differeniate between one tap and a double tap.

2      

@Hatsushira hm, how do I still get the double-column view without NavigationLink and it's destination, then?

Sorry, this is my first time using NavigationViews

2      

With the current implementation of the Sidebar in SwiftUI I don't think this is possible. The sidebar implentation with SidebarListStyle() is a very easy way to get the effect but I don't think it's fully customizable yet. I don't know about the new versions of MacOS/iPadOS and SwiftUI 3.0.

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.