WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

How to detect the user hovering over a view

Paul Hudson    @twostraws   

Updated for Xcode 14.2

In SwiftUI, any macOS app or any iPadOS app where a mouse is connected can detect when the user is hovering their pointer over a view, and respond to it appropriately.

There are two modifiers you’ll want to use: onHover(), and hoverEffect(). The first of these allows you to track whether the pointer is currently hovering over the view, and is passed a Boolean reflecting that state. For example, this will color some text red or green depending on whether the pointer is hovering over it or not:

struct ContentView: View {
    @State private var overText = false

    var body: some View {
        Text("Hello, World!")
            .foregroundColor(overText ? .green : .red)
            .onHover { over in
                overText = over

Download this as an Xcode project

The hoverEffect() modifier allows you to choose one of three ways the system can highlight the view when a hover happens: .highlight transforms the pointer into the shape of the view while creating a gentle directional effect, .lift transforms the pointer into the shape of the view while scaling up the view and placing a soft shadow behind it, and .automatic selects whichever highlight effect it thinks is most appropriate.

Note that .automatic is the default if you just apply the hoverEffect() modifier without any parameters, but it isn’t just selecting between .highlight and .lift – it’s quite a different effect, and won’t transform the pointer to match the shape of your view.

To try it out, here’s some code that places a tappable text label on the screen, giving it a .lift hover effect to make it clear that it’s tappable:

struct ContentView: View {
    var body: some View {
        Text("Tap me!")
            .onTapGesture {
                print("Text tapped")

Download this as an Xcode project

Tip: To try this out on the iPadOS simulator, go to the I/O menu and choose Input > Send Cursor to Device.

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Similar solutions…

Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.9/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.