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
Ram Patra Published on October 13, 2023
Image placeholder

Keep reading

If this article was helpful, others might be too

question swiftui April 2, 2024 How to declare an array of Views in SwiftUI?

In SwiftUI, you can declare an array of View using the standard Swift array syntax. 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 March 6, 2023 How to render the icon on a menu bar as a template in SwiftUI?

Setting your app’s icon on the menu bar as a template makes it to adapt to light and dark modes automatically without you doing anything. To do this, you have to use this constructor of MenuBarExtra class.