Skip to content

Banner_Ad_iOS_expand

AdGeneration edited this page Mar 7, 2017 · 5 revisions

iOS SDK バナー広告 端末幅いっぱいまで拡大表示するには

はじめに

開発環境にXcodeを使用することを前提とします。
本項ではAd Generation SDKにおいて320×50のサイズのバナー広告を端末幅いっぱいに拡大して表示させる方法を示します。

この方法では、バナー広告のアスペクト比を変えず端末幅まで幅と高さを等倍率で拡大させます。
端末サイズにより高さが可変となりますので、それらを考慮した画面実装が必要になります。

1. バナー広告のサイズを計算する

以下はバナー広告のサイズを計算するUIViewを拡張したクラスの実装例のです。

/**
 * BannerAdContainerView.h
 */
@interface BannerAdContainerView : UIView

@property (nonatomic, readonly)CGFloat expandRate;

- (void)matchWithScreenWidth:(NSLayoutConstraint *)heightConstraint;

@end

////////////////////////////////////////////////////////

/**
 * BannerAdContainerView.m
 */
@interface BannerAdContainerView()

@property (nonatomic, weak)NSLayoutConstraint *heightConstraint;

@end

@implementation BannerAdContainerView

static CGFloat const BANNER_BASE_WIDTH = 320;
static CGFloat const BANNER_BASE_HEIGHT = 50;

- (void)matchWithScreenWidth:(NSLayoutConstraint *)heightConstraint {
    self.heightConstraint = heightConstraint;

    CGSize windowSize = [[UIScreen mainScreen] bounds].size;
    CGFloat windowWidth = windowSize.width;
    _expandRate = windowWidth / BANNER_BASE_WIDTH;
    CGFloat height = BANNER_BASE_HEIGHT * self.expandRate;
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, windowWidth, height);

    [self setNeedsUpdateConstraints];
}

- (void)updateConstraints {
    self.heightConstraint.constant = self.frame.size.height;
    [super updateConstraints];
}

@end

2. AutoLayoutを設定する

バナー広告を配置するBannerAdContainerViewを端末幅いっぱいになるようAutoLayoutの設定をしてください。

3. 広告サイズと拡大率を指定する

ADGManagerViewControllerのinitWithAdParams:adView:で渡す幅(w)と高さ(h)及び、 ADGManagerViewControllerのsetAdScale:に比率を設定してください。

@interface ViewController ()

@property (nonatomic, weak) IBOutlet BannerAdContainerView *bannerAdView;
@property (nonatomic, weak) IBOutlet NSLayoutConstraint *bannerAdHeightConstraint;
@property (nonatomic, strong) ADGManagerViewController *adgManagerViewController;

@end

@implementation ViewController

- (void)viewDidLoad {

    // 省略

    [self.bannerAdView matchWithScreenWidth:_bannerAdHeightConstraint];

    NSDictionary *adgparam = @{
       @"locationid" : @"xxxxx",
       @"adtype" : @(kADG_AdType_Free),
       @"w" : @(self.bannerAdView.frame.size.width),
       @"h" : @(self.bannerAdView.frame.size.height)
    };
    self.adgManagerViewController = [[ADGManagerViewController alloc] initWithAdParams:adgparam
                                                                                adView:self.bannerAdView];
    [self.adgManagerViewController setFillerRetry:NO];
    [self.adgManagerViewController setAdScale:self.bannerAdView.expandRate];

    // 省略
}

注意事項

アドネットワーク事業者の配信する広告クリエイティブによっては、こちらの方法でも正しく拡大が行えなえず、実装上では制御できない可能性がございます。
以下は拡大表示の確認ができているアドネットワークです。

  • ADPRESSO
  • GunosyAds
  • i-Mobile
  • nend
  • tapone
  • Zucks

Home

導入マニュアルのドキュメントはコチラに移行しました。

お手数ですがリンクから遷移してください。 https://docs.sdk.ad-generation.jp/

Clone this wiki locally