NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

How to detect the user hovering over a view

Paul Hudson    @twostraws   

Updated for Xcode 13.3

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.

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

Buy Pro Swift 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 (Vapor Edition) 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 Server-Side Swift (Kitura Edition) 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.