2013-08-04 58 views
1

我目前使用此代碼來顯示Admob請求;設置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]]; 

我該如何改變它以顯示在屏幕的底部?

回答

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

方法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; 

     }]; 
    } 
} 
+0

嗨,這不是一個科科斯項目... –

+0

只是更新大小的作品...其他的東西都是一樣的:) – Guru

+0

嗯,我不知道你是什麼意思,我的代碼是好的我只是想將它移動到底部沒有改變各種空洞和類似的東西,沒有什麼我可以改變我的代碼塊? –

2

您就可以在谷歌廣告開發者博客提出的解決方案: 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