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 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:

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 swift September 8, 2024 How to make Squircle shape in SwiftUI and how to easily convert it to a circle or a rectangle?

To create a squircle shape (a combination of a square and a circle, also known as a superellipse) in SwiftUI, you can define a custom shape by conforming to the Shape protocol and implementing the superellipse formula. The formula for a superellipse is: