Admob banner change frame for different screen dimensions

ぐ巨炮叔叔 提交于 2020-01-04 19:43:22

问题


I'm trying to get my Admob ad to adjust between all the different iPhone screen sizes that Apple offers, but I can't figure out how to get it to adjust automatically or by using different code.

In my viewdidload

bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0, 430, 320, 50)];
bannerView_.adUnitID = @"";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
GADRequest *request = [GADRequest request];
request.testDevices = [NSArray arrayWithObjects:@"", nil ];

回答1:


You need to set the adSize property to Google's set banner size kGADAdSizeBanner. Then you set your GADBannerView's frame relative to the view. The following code places the GADBannerView on the bottom of your view.

admobBannerView.adSize = kGADAdSizeBanner;
admobBannerView.frame = CGRectMake(0, self.view.frame.size.height - admobBannerView.frame.size.height , self.view.frame.size.width, admobBannerView.frame.size.height);
  • x - Set to origin of 0
  • y - Set origin to the view's height. But this will make it appear off the bottom of the screen. So, we must subtract the height of our GADBannerView to move it up onto the screen: self.view.frame.size.height - admobBannerView.frame.size.height
  • width - Set it to the width of the view: self.view.frame.size.width
  • height - Set it to the height of our GADBannerView: admobBannerView.frame.size.height

Banner Ad Customization




回答2:


Since you are creating the banner programmatically instead of with Storyboard, I would check to find out what the height of the screen is. I use the following code to determine what phone someone is on.

CGFloat greaterPixelDimension = (CGFloat) fmaxf(((float)[[UIScreen mainScreen]bounds].size.height), ((float)[[UIScreen mainScreen]bounds].size.width));

I would then create the bannerview in the code like this:

if (greaterPixelDimension == 736) {  //6 plus
    bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0, ###, 4--, 50)];
}
else if (greaterPixelDimension == 667){  // 6
     bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0, ###, 3--, 50)];
}
else if(greaterPixelDimension == 568){  // 5
     bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0, 430, 320, 50)];
}

etc. (obviously replace the ### and 3-- with the respective points, I forget the numbers off the top of my head)



来源:https://stackoverflow.com/questions/30769378/admob-banner-change-frame-for-different-screen-dimensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!