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 March 29, 2024 How to group different style modifiers and reuse them across multiple SwiftUI views?

In SwiftUI, you can create custom view modifiers to encapsulate common styling configurations and reuse them across different views. Here’s how you can create and reuse a custom view modifier:

question swiftui swift September 2, 2024 Combine in SwiftUI and how you can rewrite the same code using async await

Combine is Apple’s declarative framework for handling asynchronous events and data streams in Swift. Introduced in SwiftUI and iOS 13, Combine leverages reactive programming principles, allowing developers to process values over time and manage complex asynchronous workflows with clarity and efficiency.

question swiftui macos September 4, 2024 How to execute some code before app termination in a macOS app using SwiftUI?

To run some code before app termination in a macOS app using SwiftUI, the correct approach would involve placing the termination logic within a view, such as the ContentView. Here’s how you can do it: