Adaptive banners

Adaptive banners are banners that fit seamlessly into user-defined block 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.

Note.

You can read about creating an ad block for an adaptive banner in the Advertising Network Help.

  1. Types of adaptive banners
  2. Enabling banner ads
  3. Example of working with adaptive banners

Types of adaptive banners

Banner with set width

Features:

  1. An alternative to 320x50 banners (when determining the banner height, the aspect ratio of 320x50 is maintained).
  2. The banner is fixed in place at the top or bottom of the screen (set up in the app).
  3. The given banner width is used instead of the device screen width. This lets you take into account the display's features.
  4. The width of adaptive banners is set using the stickySizeWithContainerWidth: method.

Examples of displaying adaptive banners:

Banner with set width and height

Features:

  1. An adaptive banner fills up the entire block using the given width and height.
  2. The width and height of adaptive banners is set with the flexibleSizeWithCGSize: method.

Examples of displaying adaptive banners:

Enabling banner ads

  1. Add the import:

    #import <YandexMobileAds/YandexMobileAds.h>
  2. Create @property, where the link to the banner ad will be stored:

    @property (nonatomic, strong) YMAAdView *adView;
  3. 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] initWithBlockID:<BlockID>
                                                    adSize:adSize
                                                  delegate:self];
    Banner with set width and height

    To set the width and height of an adaptive banner, call the flexibleSizeWithCGSize: method.

    YMAAdSize adSize = [YMAAdSize flexibleSizeWithCGSize:size];
    YMAAdView *adView = [[YMAAdView alloc] initWithBlockID:<BlockID>
                                                    adSize:adSize
                                                  delegate:self];

    BlockId is a unique identifier in the 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 - (UIViewController *)viewControllerForPresentingModalView method, links open in the in-app browser. Otherwise, links open in the browser installed on the device.

  4. Display a banner. You can place a banner, for example, using autolayout constraints.

    Usage example
    Add the banner to UIView. Then add autolayout 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];
  5. Load the banner. Optionally, you can use the YMAAdRequest class to transmit the data for targeting.

    - (void)loadAdWithRequest:(YMAAdRequest *)request;

    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] initWithBlockID:@"R-M-XXXXX-YY"
                                    adSize:adSize
                                  delegate:self];
    [self addAdView];
    
    // Loading ads
    [self.adView loadAd];
}

- (void)addAdView
{
    UIView *adView = self.adView;
    adView.translatesAutoresizingMaskIntoConstraints = false;
    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;
}