调整MoPub iOS横幅广告的大小



我需要在底部屏幕的整个宽度上分层/填充横幅广告。

我的代码适用于宽度等于MOPU_BANNER_SIZE.widthMOPUB_LEADERBOARD_SIZE.width=的设备但在其他设备上(iPhone 5/6/等和一些iPad),我唯一的选择是将横幅居中。这是代码:

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;
}

我尝试过为BannerFrameRect设置自定义尺寸,但横幅广告只是不断调整自己以适应新框架的左上角。它不会缩放。

有没有办法调整广告的大小?

感谢

请在此处查看MoPub的示例代码:

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

您可以在UIView中使用自动resizing,在您的情况下,您可以简单地将以下内容添加到代码中:

 _mopubBanner.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;

希望这能有所帮助!

您有两个可行的解决方案:

  1. 使用自动resizing掩码

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

您可能需要手动设置height,因为您只需要"水平"拉伸。

  1. 更好的IMO-使用自动布局。为此,我推荐PureLayout,因为它非常清晰和有用。

    _mopubBanner.translatesAutoresizingMaskIntoConstraints = NO;
    [_mopubBanner autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeTop];
    

最新更新