Resize MoPub iOS banner ad?
Asked Answered
S

2

6

I need to have my banner ads stratched/filled all across the width of the screen at the bottom.

My code is working for the devices with the width equal to either MOPUB_BANNER_SIZE.width or MOPUB_LEADERBOARD_SIZE.width But on other devices (iPhone 5/6/etc and some iPads) my only option is to center the banner. here is the code:

if(!_mopubBanner){
    NSString* bannerID;
    const CGSize* bannerSize;
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
        bannerID = @"MopubBannerId";
        bannerSize = &MOPUB_BANNER_SIZE;
    }
    else{
        bannerID = @"MopubLeaderboardId";
        bannerSize = &MOPUB_LEADERBOARD_SIZE;
    }

    _mopubBanner = [[MPAdView alloc] initWithAdUnitId:bannerID size:*bannerSize];
    _mopubBanner.delegate = self;

    CGRect BannerFrameRect;
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        BannerFrameRect = CGRectMake(([[UIScreen mainScreen] bounds].size.width - MOPUB_BANNER_SIZE.width) / 2,
                                    [[UIScreen mainScreen] bounds].size.height - MOPUB_BANNER_SIZE.height,
                                    MOPUB_BANNER_SIZE.width,
                                    MOPUB_BANNER_SIZE.height);
    }
    else
    {
        BannerFrameRect = CGRectMake(([[UIScreen mainScreen] bounds].size.width - MOPUB_LEADERBOARD_SIZE.width) / 2,
                                    [[UIScreen mainScreen] bounds].size.height - MOPUB_LEADERBOARD_SIZE.height,
                                    MOPUB_LEADERBOARD_SIZE.width,
                                    MOPUB_LEADERBOARD_SIZE.height);
    }

    _mopubBanner.frame = BannerFrameRect;
}

I have tried setting custom sizes for BannerFrameRect but the banner ad just keeps adjusting itself to the top left corner of the new frame. It does not get scaled.

is there any way to resize the ad?

thank you

Soubrette answered 28/10, 2015 at 13:59 Comment(0)
C
0

please take a look at the MoPub's sample code here:

https://github.com/mopub/mopub-ios-sdk/blob/master/MoPubSampleApp/Controllers/MPLeaderboardBannerAdDetailViewController.m

You can use autoresizing in the UIView, and in your case you could simply add the following to your code:

 _mopubBanner.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;

hope this helps!

Cookson answered 3/1, 2016 at 21:6 Comment(0)
S
0

You have two viable solutions:

  1. Use autoresizing masks

    _mopubBanner.translatesAutoresizingMaskIntoConstraints = YES;
    _mopubBanner.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    

You'll probably need to set height manually as you only want 'horizontal' stretch.

  1. Much better IMO - use Autolayouts. For this I'd recommend PureLayout as it's really clear and useful.

    _mopubBanner.translatesAutoresizingMaskIntoConstraints = NO;
    [_mopubBanner autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeTop];
    
Scurvy answered 4/1, 2016 at 16:6 Comment(3)
thanks for a comprehensive answer. Unfortunately I'm on a vacation right now so I can't test it. I won't accept the answer till I'll be able to test it. But I'll give you the bounty as a little encouragement to be more active on SO. :)) thanks.Soubrette
Thanks, write back after testing, I can help you with Autolayouts too.Scurvy
tested this out - mopub still refuses to get resized. Well, hell with it, as long as it doesn't crash :))Soubrette

© 2022 - 2024 — McMap. All rights reserved.