Article

Optimizing Your iPad App for Mac

Make your iPad app more like a Mac app by taking advantage of system features in macOS.

Overview

The Mac version of your iPad app supports many system features found in macOS without requiring any effort from you. These features include:

  • A default menu bar for your app.

  • Support for trackpad, mouse, and keyboard input.

  • Support for window resizing and full-screen display.

  • Mac-style scroll bars.

  • Copy-and-paste support.

  • Drag-and-drop support.

  • Support for system Touch Bar controls.

You can, however, extend your app to take advantage of even more system features.

Add Menu Bar Items

The Mac version of your app comes with a standard menu bar. Customize it by adding and removing menu items using UIMenuBuilder.

Show a Preferences Window

Mac apps typically let users manage app-specific settings by displaying a preferences window. Users see this window by selecting the app menu followed by the Preferences menu item in the menu bar. If your app has a Settings bundle, the system automatically provides your app with a preferences window. To learn more, see Displaying a Preferences Window.

Apply a Translucent Background to Your Primary View Controller

iPad apps using a split view controller get a Mac-style vertical split view when running in macOS. But to make your iPad app look more at home on Mac, apply a translucent effect that blurs the desktop into the primary view controller's background. To do this, set your split view controller’s primaryBackgroundStyle to UISplitViewController.BackgroundStyle.sidebar, as shown in Listing 1.

Listing 1

Add a translucent background to the primary view controller

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    let splitViewController = window!.rootViewController as! UISplitViewController
    let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
    navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
    
    // Add a translucent background to the primary view controller.
    splitViewController.primaryBackgroundStyle = .sidebar
    
    splitViewController.delegate = self
    
    return true
}

Detect the Pointer in a View

Mac users rely on a pointer to interact with apps, whether selecting a text field or moving a window. As the user moves the pointer over UI elements, some elements should change their appearance. For example, a web browser highlights a link as the pointer moves over it.

To detect when the user moves the pointer over a view in your app, add a UIHoverGestureRecognizer to that view. This tells your app when the pointer enters or leaves the view, or moves while over it.

Listing 2

Change the button's default color to red as the pointer moves over the button

class ViewController: UIViewController {

    @IBOutlet var button: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        let hover = UIHoverGestureRecognizer(target: self, action: #selector(hovering(_:)))
        button.addGestureRecognizer(hover)
    }

    @objc
    func hovering(_ recognizer: UIHoverGestureRecognizer) {
        switch recognizer.state {
        case .began, .changed:
            button.titleLabel?.textColor = #colorLiteral(red: 1, green: 0, blue: 0, alpha: 1)
        case .ended:
            button.titleLabel?.textColor = UIColor.link
        default:
            break
        }
    }
}

See Also

Mac Optimizations

Displaying a Preferences Window

Let users manage app settings defined in a Settings bundle through a system-provided preferences window.