Set Admob banner to appear at bottom of screen

后端 未结 2 815
抹茶落季
抹茶落季 2020-12-20 05:57

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;

CGRe         


        
相关标签:
2条回答
  • 2020-12-20 06:07

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

    METHOD 2: Detailed clean way

    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;
    
             }];
        }
    }
    
    0 讨论(0)
  • 2020-12-20 06:27

    You can check the solution proposed in Google Ads developer blog: http://googleadsdeveloper.blogspot.com/2012/06/keeping-smart-banner-docked-to-bottom.html

    // Initialize the banner docked to the bottom of the screen.
    // We start in a portrait orientation so use kGADAdSizeSmartBannerPortrait.
      CGPoint origin = CGPointMake(0.0,
                                   self.view.frame.size.height -
                                   CGSizeFromGADAdSize(
                                       kGADAdSizeSmartBannerPortrait).height);
    
      self.adBanner = [[[GADBannerView alloc]
                        initWithAdSize:kGADAdSizeSmartBannerPortrait
                                origin:origin] autorelease];
    
    //Continue rest of initialization here
    

    Make sure to use self.view.frame.size.width in the Y coordinate of the origin if you are using kGADAdSizeSmartBannerLandscape.

    0 讨论(0)
提交回复
热议问题