Adaptive banners
This is an archived version of the documentation. Actual documentation for all platforms can be found here.
Adaptive banners are banners that fit seamlessly into user-defined unit sizes. Depending on how an adaptive banner is integrated, the optimal height is determined for the given width or the specified size of ad placement is used.
You can read about creating an ad unit for an adaptive banner in the Advertising Network Help.
Types of adaptive banners
- An alternative to 320x50 banners (when determining the banner height, the aspect ratio of 320x50 is maintained).
- The banner is fixed in place at the top or bottom of the screen (set up in the app).
- The given banner width is used instead of the device screen width. This lets you take into account the display's features.
- The width of adaptive banners is set using the +stickySizeWithContainerWidth: method.
- An adaptive banner fills up the entire unit using the given width and height.
- The width and height of adaptive banners is set using the +flexibleSizeWithCGSize: method.
Features:
Examples of displaying adaptive banners:
Features:
Examples of displaying adaptive banners:
Enabling banner ads
Add the import:
import YandexMobileAds
#import <YandexMobileAds/YandexMobileAds.h>
Create @property, where the link to the banner ad will be stored:
var adView: YMAAdView!
@property (nonatomic, strong) YMAAdView *adView;
- Specify the banner size and initialize the banner in the app.Banner with set width
To set the width of an adaptive banner, call the +stickySizeWithContainerWidth: method.
YMAAdSize adSize = [YMAAdSize stickySizeWithContainerWidth:width]; YMAAdView *adView = [[YMAAdView alloc] initWithAdUnitID:<AdUnitID> adSize:adSize]; adView.delegate = self;
Banner with set width and heightTo set the width and height of an adaptive banner, call the +flexibleSizeWithCGSize: method.
YMAAdSize adSize = [YMAAdSize flexibleSizeWithCGSize:size]; YMAAdView *adView = [[YMAAdView alloc] initWithAdUnitID:<AdUnitID> adSize:adSize]; adView.delegate = self;
AdUnitId
is a unique identifier in R-M-XXXXXX-Y format, which is assigned in the Partner interface.In addition,
self
must conform to the YMAAdViewDelegate protocol. If the delegate implements the -viewControllerForPresentingModalView method, links open in the in-app browser. Otherwise, links open in the browser installed on the device. Display a banner. You can place a banner, for example, using
autolayout constraints
.Usage exampleAdd the banner toUIView
. Then addautolayout constraints
so the banner is displayed in the desired location.[self.view addSubview:self.adView]; self.adView.translatesAutoresizingMaskIntoConstraints = false; UIView *adView = self.adView; NSDictionary<NSString *, UIView *> *views = NSDictionaryOfVariableBindings(adView); NSArray<NSLayoutConstraint *> *horizontal = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[adView]|" options:0 metrics:nil views:views]; NSArray<NSLayoutConstraint *> *vertical = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[adView]-|" options:0 metrics:nil views:views]; [self.view addConstraints:horizontal]; [self.view addConstraints:vertical];
Load the banner. Optionally, you can use the YMAAdRequest class to transmit the data for targeting.
When the banner loads, the delegate is notified.
Example of working with adaptive banners
The following code demonstrates creating and configuring the AdView
object, registering a listener, and loading an adaptive banner:
- (void)viewDidLoad
{
[super viewDidLoad];
// Setting the ad width
YMAAdSize *adSize =
[YMAAdSize stickySizeWithContainerWidth:[self containerWidth]];
// Creating an adView instance
self.adView = [[YMAAdView alloc] initWithAdUnitID:@"R-M-XXXXX-YY" adSize:adSize];
self.adView.delegate = self;
[self addAdView];
// Loading ads [self.adView loadAd];
}
- (void)addAdView
{
UIView *adView = self.adView;
adView.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary<NSString *, UIView *> *views = NSDictionaryOfVariableBindings(adView);
[self.view addSubview:adView];
NSArray<NSLayoutConstraint *> *horizontal =
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[adView]|"
options:0
metrics:nil
views:views];
NSArray<NSLayoutConstraint *> *vertical =
[NSLayoutConstraint constraintsWithVisualFormat:@"V:[adView]-|"
options:0
metrics:nil
views:views];
[self.view addConstraints:horizontal];
[self.view addConstraints:vertical];
}
- (CGFloat)containerWidth
{
CGFloat containerWidth = self.view.frame.size.width;
if (@available(iOS 11.0, *)) {
containerWidth =
UIEdgeInsetsInsetRect(self.view.frame, self.view.safeAreaInsets).size.width;
}
return containerWidth;
}