Material Design

Overlay window

Provides a window which can have an arbitrary number of overlay views that will sit above the root view of the window. Overlays will be the full size of the screen, and will be rotated as appropriate based on device orientation. For performance, owners of overlay views should set the |hidden| property to YES when the overlay is not in use.

Overlay Window is used by components such as Snackbar. Snackbar uses Overlay Window to ensure displayed message views are always visible to the user by being at the top of the view hierarchy.

Installation

Installation with CocoaPods

To add this component to your Xcode project using CocoaPods, add the following to your Podfile:

pod 'MaterialComponents/OverlayWindow'

Then, run the following command:

pod install

Usage

Importing

Before using the Overlay Window, you’ll need to import it:

Swift

import MaterialComponents.MaterialOverlayWindow

Objective-C

#import "MaterialOverlayWindow.h"

Examples

Setting the Overlay Window

Using the Overlay Window requires that the App Delegate set the window as an Overlay Window or a subclass of Overlay Window.

Swift

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

  window = MDCOverlayWindow(frame: (application.keyWindow?.bounds)!)

}

Objective-C

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  [[MDCOverlayWindow alloc] initWithFrame:application.keyWindow.bounds];

}

Using the Overlay Window

Once the Overlay Window is set in the App Delegate, the client can use the Overlay Window to display views at the top most level of the view hierarchy.

Swift

// Set up view to be displayed in the overlay window.
let myOverlayView = UIView()
...

// When you're ready to show the overlay, activate it
if let overlayWindow = window as? MDCOverlayWindow {
  overlayWindow.activateOverlay(myOverlayView, withLevel:UIWindowLevelNormal)
}

Objective-C

// Set up view to be displayed in the overlay window.
UIView *overlayView = [[UIView alloc] init];
...

// When you're ready to show the overlay, activate it
if ([self.window isKindOfClass:[MDCOverlayWindow class]]) {
  MDCOverlayWindow *overlayWindow = (MDCOverlayWindow *)self.window;
  [overlayWindow activateOverlay:overlayView withLevel:UIWindowLevelNormal];
}