1

現在、このコードを使用して Admob リクエストを表示しています。

このコードを使用すると、バナーが表示されますが、画面の下部ではなく上部に表示されます。

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

これを変更して、代わりに画面の下部に表示するにはどうすればよいですか?

4

2 に答える 2

5

方法 1: このコードをコードの最後に挿入します。

//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: 詳細なきれいな方法

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;

         }];
    }
}
于 2013-08-04T14:28:05.793 に答える
2

Google 広告デベロッパー ブログで提案されているソリューションを確認できます: 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

を使用する場合は、必ずself.view.frame.size.width原点の Y 座標で使用してくださいkGADAdSizeSmartBannerLandscape

于 2014-08-25T13:16:10.590 に答える