6

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

我的代码适用于宽度等于MOPUB_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 设置自定义尺寸,但横幅广告只是不断调整自己到新框架的左上角。它不会被缩放。

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

谢谢你

4

2 回答 2

0

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

  1. 使用自动调整大小的蒙版

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

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

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

    _mopubBanner.translatesAutoresizingMaskIntoConstraints = NO;
    [_mopubBanner autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeTop];
    
于 2016-01-04T16:06:21.940 回答
0

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

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

您可以在 UIView 中使用自动调整大小,在您的情况下,您只需将以下内容添加到您的代码中:

 _mopubBanner.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;

希望这可以帮助!

于 2016-01-03T21:06:20.907 回答