Loading...

How to change the cursor to a hand when hovering over a Link View in SwiftUI?

question swiftui
Ram Patra Published on October 13, 2023
import SwiftUI

extension Link {
    func pointingHandCursor() -> some View {
        self.onHover { inside in
            if inside {
                NSCursor.pointingHand.set()
            } else {
                NSCursor.arrow.set()
            }
        }
    }
}

In this code, we’ve created an extension for Link called pointingHandCursor() that sets the cursor behavior. You can use this extension to apply the cursor change to any Link in your SwiftUI views, making it more convenient to use in your app like below:

Link(destination: URL(string: "https://www.example.com")!) {
    Text("Visit Example.com")
}
.pointingHandCursor() // Use the extension to set the cursor to a hand
Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on October 13, 2023
Image placeholder

Keep reading

If this article was helpful, others might be too

question swiftui macOS January 26, 2024 How to hide the title bar in a SwiftUI macOS app?

Let’s say you have a view named ContentView and your main App file looks like this:

question swiftui macos October 15, 2023 How to display the app version and build number in a macOS/iOS SwiftUI app?

To display both the app version and build number in a SwiftUI macOS/iOS app, you can use the Bundle class to access information from the app’s Info.plist file. The Info.plist file contains various details about your application, including its version and build number. Here’s how you can do it:

question swiftui macos September 3, 2024 How to open and close windows programmatically in SwiftUI?

To open or close a window programmatically from outside that window using environment variables, you need to leverage the new openWindow (macOS 13+) and dismissWindow (macOS 14+) environment variables. This environment variables allow you to programmatically open and close a window by its identifier.

Like my work?

Please, feel free to reach out. I would be more than happy to chat.