将iAd横幅覆盖在屏幕上而不是调整屏幕大小

时间:2015-04-21 06:18:47

标签: ios iphone swift iad

我使用Swift制作了两个iPhone应用,并且刚刚实施了iAd框架,并在self.canDisplayBannerAds = true viewDidLoad()下的GameViewController()添加了{{1}}。

当广告出现在我的游戏中时,游戏屏幕会调整大小,以便所有内容仍然可见。当发生这种情况时,游戏屏幕会垂直缩小,游戏变得更加困难。

如何在不调整游戏画面大小的情况下将横幅广告叠加到屏幕底部?我希望游戏画面的底部部分根本不可见。

3 个答案:

答案 0 :(得分:1)

  

据我所知,问题出在self.canDisplayBannerAds = true

这段代码对我来说非常合适!请确保您将AdBannerView命名为“adBanner”(var adBanner = ADBannerView()

viewDidLoad

中加入此代码
        adBanner = ADBannerView()
        adBanner.frame = CGRectZero
        adBanner.delegate = self
        self.adBanner.frame = CGRectMake(0, self.view.frame.size.height-self.adBanner.frame.size.height, self.adBanner.frame.size.width, self.adBanner.frame.size.height)
        adBanner.backgroundColor = UIColor.clearColor()
        self.view.addSubview(adBanner)

答案 1 :(得分:0)

试试吧......

// Import iAd and AdMob in your header file
#import "ViewController.h"
// Enter YOUR ad id you receive from AdMob here
#define BANNER_UNIT_ID @"yourAdMobBannerID"

@interface ViewController ()
@end

@implementation ViewController {
    //We will put these here so we can access them globally
    GADBannerView *adMobView;
    ADBannerView *iAdView;
    CGRect screenBounds;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Get device screen size
    // For example, screenBounds on an iPhone 6 will look like this
    // screenBounds.origin.x == 0
    // screenBounds.origin.y == 0
    // screenBounds.size.width == 375
    // screenBounds.size.height == 667
    screenBounds = [[UIScreen mainScreen] bounds];

    // Setup iAd view
    // Create the AdBannerView
    iAdView = [[ADBannerView alloc] initWithFrame:CGRectZero];

    // Set its delegate
    iAdView.delegate=self;

    // This sets the frame origin at (0,0) which would be the top left of the device screen
    // iAdView.bounds.size.width and iAdView.bounds.size.height sets the size of the AdBannerView
    [iAdView setFrame:CGRectMake(0, 0, iAdView.bounds.size.width, iAdView.bounds.size.height)];

    // This will take the center of our AdBannerView and move it to a point (x,y)
    // We want our AdBannerView.center in the center of the device screen
    // So lets get the width of our screen and divide it by 2. We do this with screenBounds.size.width / 2
    // We also want our AdBannerView to be at the bottom of the screen
    // So lets get the height of our screen with screenBounds.size.height
    // Remember were talking about the center of our AdBannerView here so if we just set it to that
    // Half of our AdBannerView's height will be cut off by the bottom of the screen
    // So lets subtract half of our AdBannerView's height to fix that with iAdView.bounds.size.height / 2
    iAdView.center = CGPointMake(screenBounds.size.width / 2, screenBounds.size.height - (iAdView.bounds.size.height / 2));

    // Add it to our view
    [self.view addSubview:iAdView];

    // Our AdBannerView is now at the bottom of our devices screen
    // But it takes a second to receive an ad from iAd's network so lets make it transparent for now
    iAdView.alpha = 0.0;


    // Setup AdMob view
    // Create the GADBannerView
    adMobView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];

    // Use the BANNER_UNIT_ID we defined earlier
    adMobView.adUnitID = BANNER_UNIT_ID;
    adMobView.rootViewController = self;
    [adMobView loadRequest:[GADRequest request]];

    // This sets the frame origin at (0,0) which would be the top left of the device screen
    // adMobView.bounds.size.width and adMobView.bounds.size.height sets the size of the GADBannerView
    [adMobView setFrame:CGRectMake(0, 0, adMobView.bounds.size.width, adMobView.bounds.size.height)];

    // This will take the center of our GADBannerView and move it to a point (x,y)
    // We want our GADBannerView.center in the center of the device screen
    // So lets get the width of our screen and divide it by 2. We do this with screenBounds.size.width / 2
    // We also want our GADBannerView to be at the bottom of the screen
    // So lets get the height of our screen with screenBounds.size.height
    // Remember were talking about the center of our GADBannerView here so if we just set it to that
    // Half of our GADBannerView's height will be cut off by the bottom of the screen
    // So lets subtract half of our GADBannerView's height to fix that with adMobView.bounds.size.height / 2
    adMobView.center = CGPointMake(screenBounds.size.width / 2, screenBounds.size.height - (adMobView.bounds.size.height / 2));

    // Add it to our view
    [self.view addSubview:adMobView];
    // We don't have to set the alpha of GADBannerView to 0.0 because it is automatically transparent if no ad is available
}

//iAd methods
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
    NSLog(@"iAd received ad");
    // We received an ad from iAd so lets show it
    // We will animate the transition of its alpha from 0.0 to 1.0
    // Also lets animate our GADBannerView from 1.0 to 0.0 to hide it
    [UIView beginAnimations:nil context:NULL];
    iAdView.alpha = 1.0;
    adMobView.alpha = 0.0;
    [UIView commitAnimations];
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    NSLog(@"iAd failed");
    // We failed to receive an ad from iAd so lets hide it
    // We will animate the transition of its alpha from 1.0 to 0.0
    // Also lets animate our GADBannerView from 0.0 to 1.0 to show it
    // AdMob has near a 100% fill rate so the chances of there being an ad are almost certain
    [UIView beginAnimations:nil context:NULL];
    iAdView.alpha = 0.0;
    adMobView.alpha = 1.0;
    [UIView commitAnimations];
}

答案 2 :(得分:0)

在Objective-C中使用SpriteKit,我可以在底部添加横幅,而无需调整原始内容的大小:

#ifdef USING_iAD
//  setup iAds
self.canDisplayBannerAds = YES;
self.canDisplayBannerAds = NO;
self.interstitialPresentationPolicy = ADInterstitialPresentationPolicyManual;

//  configure the view
SKView *skView = [[SKView alloc] initWithFrame:self.originalContentView.frame];
[self.originalContentView addSubview:skView];

#else
//  this was the original code
SKView *skView = (SKView *)self.view;
#endif

//skView.showsFPS = YES;
//skView.showsNodeCount = YES;
//skView.showsPhysics = YES;
skView.ignoresSiblingOrder = YES;