A banner view periodically retrieves advertisements from the iAd service and displays them to the user. Your application dedicates a small portion of a user interface screen to a banner view, as shown in Figure 1-1.
Advertisements define an action that takes place when the user taps on the banner. For example, an advertisement could launch another application or temporarily cover your application’s user interface to play a movie or to present an interactive advertisement. Figure 1-2 shows the screen after the user taps the display.
A key point is that the user decides when they want to see the content associated with the banner advertisement. The user remains in control.
Any user interface screen that includes a banner view must be managed by a view controller (a class that subclasses UIViewController
). This allows a triggered action to cover your user interface with an additional advertising screen. Whenever a banner view is visible, it must be part of a view hierarchy that is attached to the view
property of a view controller. The simplest way to do this is to instantiate the view as part of the nib file used to instantiate the view controller’s interface.
Listing 1-1 shows the simplest code that a view controller might use to programmatically create a banner view in portrait mode.
Listing 1-1 Programmatically creating a portrait banner view
ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectZero]; |
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50; |
[self.view addSubview:adView]; |
Every advertisement provides one or more images that can be displayed inside the view. iAd supports two image sizes: 320 x 50 points for a portrait advertisement, and 480 x 32 points for a landscape advertisement. In the future, additional sizes may be exposed by iAd.
To ensure that advertisements are displayed properly, a banner view must always be sized to match one of the built-in advertising sizes. The proper way to do this is to set the currentContentSizeIdentifier
property. Changing this property resizes the banner view’s frame to the match this content size. The size identifier must also be included in the set attached to the requiredContentSizeIdentifiers
property. For example, Listing 1-2 shows how your view controller could programmatically create a landscape banner view.
Listing 1-2 Programmatically creating a landscape banner view
ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectZero]; |
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier480x32]; |
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32; |
[self.view addSubview:adView]; |
A banner view is a user interface element. As with other user interface elements, you should only reference it from your application’s main thread.
Last updated: 2010-05-27