A class that provides the infrastructure for managing the interface in a watchOS app.
- watchOS 2.0+
An interface controller serves the same purpose as a
UIView object in a UIKit app, except that it does not manage any actual views. It runs in your WatchKit extension and remotely manages the behavior associated with an interface controller in your Watch app’s storyboard file. You subclass
WKInterface and use its methods to configure the elements of your storyboard scene and to respond to interactions with those elements.
Your interface controller code runs locally on the user’s Apple Watch but is separate from the interface that it manages. When you change the value of an interface object in your code, the system forwards the needed information to your Watch app, which makes the corresponding changes onscreen.
Initialize Your Interface Controllers
When the user interacts with your app content, the system launches your extension and creates the appropriate interface controller objects automatically. Apps use different interface controllers to manage their notification and app interfaces; WatchKit uses the information in your app’s main storyboard file to determine which interface controller to load. Notification scenes are configured specially so that the system can identify them. For your app, WatchKit loads your app’s main interface controller initially, but you may change the initial interface controller at launch time.
When creating an interface controller, WatchKit instantiates the class and calls its
init() method. You can use this method to initialize variables and load data; however, don't use it to configure your user interface. The controller's user interface elements may not be properly initialized when this method runs.
Next, the system calls the
awake(with method. If WatchKit passes a valid object to the
awake(with method, use the information in that object to customize the initialization process. Also, the controller's user interface elements are guaranteed to be available at this point. This means that you can safely use this method to configure your user interface.
will method lets you know when your interface is about to become active. Use the
will method to perform any last minute tasks, such as checking for updates to your content; however, don't use it for your primary initialization.
will method may be called at times when your interface is not yet onscreen. For example, WatchKit may call the method in advance so that you have time to update your content. WatchKit calls the
did method to let you know when your interface becomes visible. Similarly, WatchKit calls the
did methods when your interface moves offscreen again.
In iOS Simulator, WatchKit calls the
did method for the current interface controller when you lock the simulator by selecting Hardware > Lock. When you subsequently unlock the simulator, WatchKit calls that interface controller’s
will method again. You can use this capability to debug your activation and deactivation code.
Interface Builder Configuration Options
Xcode lets you configure information about your interface controller in your storyboard file. Table 1 lists the attributes you can configure in your storyboard and their meaning.
The name of the interface controller. Use this name to specify which interface controller to push or present.
The title string assigned to the interface controller. You can set this value programmatically using the
Is Initial Controller
A Boolean indicating whether the object is the app’s root interface controller. Only one interface controller at a time may have this option enabled. This option does not apply to glance or notification interface controllers.
Activity Indicator On Load
A Boolean indicating whether the interface controller’s contents should be hidden until the
A Boolean value that turns off scrolling and allows built-in controls and containers to fill content to the screen edges, regardless of the content-safe area.
A Boolean value that determines whether SpriteKit or SceneKit content can use the full screen. The system hides the status bar but displays the time in the upper-right corner with a gradient behind it, making the time clearly visible against the scene.
Fixed to screen edges
A Boolean value that indicates whether content should ignore the safe area and minimum layout margins. When this option is enabled, the system turns off scrolling, and allows built-in controls and containers to fill content to the screen edges.
The background image displayed behind the scene’s content. The image specified in your storyboard scrolls with your interface controller’s content.
The content mode for the background image. This mode defines how the background image scales or fills the screen and behaves in the same way as the constants for the
A Boolean value indicating whether an animated background image starts running its animation automatically after being loaded. Set this option to Yes if you want the animation to start automatically; set it to No if you prefer to start the animation programmatically.
The background color to be displayed behind the scene’s content.
The amount of space (in points) to insert between the edges of the interface controller and its content. Selecting Custom lets you specify different values for the top, bottom, left, and right edges.
Additional spacing (in points) to include between items in the interface controller.
WKInterface when you have a storyboard scene that requires configuration at runtime or that handles user interactions. Typically, you define a custom subclass for each unique storyboard scene that your app manages. In your subclass, define outlets for any interface objects you need to configure and define action methods for responding to interactions with the elements of your storyboard scene.
Most custom interface controllers you use in your app require a custom interface controller subclass. Even glances need an interface controller to update the glance contents. The only storyboard scene that cannot use a custom interface controller is the scene associated with a static notification interface. When implementing an interface controller for your dynamic notification interface, subclass
Override any methods of the class needed to configure your interface and get it ready to display. Most interface controllers override the
awake(with methods. Override any other methods that make sense based on your needs.