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

Nika Kasradze
  • 2,834
  • 3
  • 25
  • 48

2 Answers2

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!

Allen
  • 6,505
  • 16
  • 19
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];
    
Krodak
  • 1,493
  • 14
  • 21
  • 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. – Nika Kasradze Jan 05 '16 at 11:01
  • Thanks, write back after testing, I can help you with Autolayouts too. – Krodak Jan 05 '16 at 13:35
  • tested this out - mopub still refuses to get resized. Well, hell with it, as long as it doesn't crash :)) – Nika Kasradze Jan 14 '16 at 15:22