将 Admob 横幅设置为显示在屏幕底部

Set Admob banner to appear at bottom of screen(将 Admob 横幅设置为显示在屏幕底部)

本文介绍了将 Admob 横幅设置为显示在屏幕底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用此代码来显示 Admob 请求;

I am currently using this code to display an Admob request;

如果我使用此代码,横幅会出现,但在屏幕顶部而不是底部;

If I use this code, the banner appears, but at the top of the screen not the bottom;

CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;
    CGFloat screenXPos = (screenWidth/2);
    CGFloat screenYPos = screenHeight - kGADAdSizeBanner.size.height;
    [bannerView_ setCenter:CGPointMake(screenXPos, screenYPos)];
    bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerLandscape];
    bannerView_.adUnitID = ADMOB_ID;
    bannerView_.rootViewController = self;
    bannerView_.adUnitID = ADMOB_ID;
    bannerView_.rootViewController = self;
    [self.view addSubview:bannerView_];
    [bannerView_ loadRequest:[GADRequest request]];

如何将其更改为显示在屏幕底部?

How would I change this to show at the bottom of the screen instead?

推荐答案

方法一:把这段代码放在代码的底部.

METHOD 1: Put this code in bottom of your code.

//TRY_1:
        bannerView_.frame = CGRectMake(screenWidth/2.0 - bannerView_.frame.size.width/2.0, screenHeight - bannerView_.frame.size.height,
                                       bannerView_.frame.size.width,bannerView_.frame.size.height);
//OR TRY_2:
        bannerView_.frame = CGRectMake(screenHeight/2.0 - bannerView_.frame.size.width/2.0, screenWidth - bannerView_.frame.size.height,
                                       bannerView_.frame.size.width,bannerView_.frame.size.height);

方法2:详细清洁方式

typedef enum _bannerType
{
    kBanner_Portrait_Top,
    kBanner_Portrait_Bottom,
    kBanner_Landscape_Top,
    kBanner_Landscape_Bottom,
}CocosBannerType;

//In .h declare  CocosBannerType mBannerType;

-(void)createAdmobAds
 {
    mBannerType = kBanner_Portrait_Bottom; //kBanner_Portrait_Top

    AppController *app =  (AppController*)[[UIApplication sharedApplication] delegate];
    // Create a view of the standard size at the bottom of the screen.
    // Available AdSize constants are explained in GADAdSize.h.

    if(mBannerType <= kBanner_Portrait_Bottom)
        mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
    else
        mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerLandscape];

    // Specify the ad's "unit identifier." This is your AdMob Publisher ID.
    mBannerView.adUnitID = MY_BANNER_UNIT_ID;

    // Let the runtime know which UIViewController to restore after taking
    // the user wherever the ad goes and add it to the view hierarchy.

    mBannerView.rootViewController = app.navController;
    [app.navController.view addSubview:mBannerView];

    // Initiate a generic request to load it with an ad.
    [mBannerView loadRequest:[GADRequest request]];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGSize s = screenRect.size;

    CGRect frame = mBannerView.frame;

    off_x = 0.0f;
    on_x = 0.0f;

    switch (mBannerType)
    {
        case kBanner_Portrait_Top:
        {
            off_y = -frame.size.height;
            on_y = 0.0f;
        }
            break;
        case kBanner_Portrait_Bottom:
        {
            off_y = s.height;
            on_y = s.height-frame.size.height;
        }
            break;
        case kBanner_Landscape_Top:
        {
            off_y = -frame.size.height;
            on_y = 0.0f;
        }
            break;
        case kBanner_Landscape_Bottom:
        {
            off_y = s.height;
            on_y = s.height-frame.size.height;
        }
            break;

        default:
            break;
    }

    frame.origin.y = off_y;
    frame.origin.x = off_x;

    mBannerView.frame = frame;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    frame = mBannerView.frame;
    frame.origin.x = on_x;
    frame.origin.y = on_y;


    mBannerView.frame = frame;
    [UIView commitAnimations];
}


-(void)showBannerView
{
    if (mBannerView)
    {
        [UIView animateWithDuration:0.5
                              delay:0.1
                            options: UIViewAnimationCurveEaseOut
                         animations:^
         {
             CGRect frame = mBannerView.frame;
             frame.origin.y = on_y;
             frame.origin.x = on_x;

             mBannerView.frame = frame;
         }
                         completion:^(BOOL finished)
         {
         }];
    }

}


-(void)hideBannerView
 {
    if (mBannerView)
    {
        [UIView animateWithDuration:0.5
                              delay:0.1
                            options: UIViewAnimationCurveEaseOut
                         animations:^
         {
             CGRect frame = mBannerView.frame;
             frame.origin.y = off_y;
             frame.origin.x = off_x;
         }
                         completion:^(BOOL finished)
         {
         }];
    }

}

-(void)dismissAdView
 {
    if (mBannerView) 
    {
        [UIView animateWithDuration:0.5
                              delay:0.1
                            options: UIViewAnimationCurveEaseOut
                         animations:^
         { 
             CGRect frame = mBannerView.frame;
             frame.origin.y = off_y;
             frame.origin.x = off_x;
             mBannerView.frame = frame;
         } 
                         completion:^(BOOL finished)
         {
             [mBannerView setDelegate:nil];
             [mBannerView removeFromSuperview];
             mBannerView = nil;

         }];
    }
}

这篇关于将 Admob 横幅设置为显示在屏幕底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将 Admob 横幅设置为显示在屏幕底部

基础教程推荐