iOS Reference Library Apple Developer
Search

Banner View Concepts

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.

Figure 1-1  An advertisement in a banner view

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.

Figure 1-2  A banner action

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.

Banner Views Require a View Controller

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.

Creating a Banner View

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];

Banner View Sizes

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.

image: ../Art/ad_sizes.jpg

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];

Thread Safety

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

Did this document help you? Yes It's good, but... Not helpful...