我可以在 iphone 中显示具有自定义尺寸的 admob 广告,例如 280x50,而不是通常的 320x50 吗?
8 回答
自定义广告尺寸
除了标准的 AdMob 广告单元外,DFP 还允许您将任何尺寸的广告单元投放到应用程序中。请注意,为广告请求定义的广告尺寸(宽度、高度)应与应用程序上显示的广告视图的尺寸(即 DFPBannerView)相匹配。
例子:
// Define custom GADAdSize of 280x30 for DFPBannerView
GADAdSize customAdSize = GADAdSizeFromCGSize(280, 30);
// Don't use autorelease if you are using ARC in your project
self.adBanner = [[[DFPBannerView alloc] initWithAdSize:customAdSize] autorelease];
注意:DFP 目前不支持智能横幅。
多种广告尺寸
DFP 允许您指定可能有资格投放到 DFPBannerView 中的多种广告尺寸。使用此功能需要三个步骤:
在 DFP 用户界面中,创建一个定位到与不同尺寸广告素材相关联的同一广告单元的订单项。在您的应用程序中,设置 DFPBannerView 上的 validAdSizes 属性:
// Define an optional array of GADAdSize to specify all valid sizes that are appropriate
// for this slot. Never create your own GADAdSize directly. Use one of the
// predefined standard ad sizes (such as kGADAdSizeBanner), or create one using
// the GADAdSizeFromCGSize method.
//
// Note: Ensure that the allocated DFPBannerView is defined with an ad size. Also note
// that all desired sizes should be included in the validAdSizes array.
GADAdSize size1 = GADAdSizeFromCGSize(CGSizeMake(120, 20));
GADAdSize size2 = GADAdSizeFromCGSize(CGSizeMake(250, 250));
GADAdSize size3 = GADAdSizeFromCGSize(CGSizeMake(320, 50));
NSMutableArray *validSizes = [NSMutableArray array];
[validSizes addObject:[NSValue valueWithBytes:&size1 objCType:@encode(GADAdSize)]];
[validSizes addObject:[NSValue valueWithBytes:&size2 objCType:@encode(GADAdSize)]];
[validSizes addObject:[NSValue valueWithBytes:&size3 objCType:@encode(GADAdSize)]];
bannerView_.validAdSizes = validSizes;
实施 GADAdSizeDelegate 方法来检测广告尺寸变化。
@protocol GADAdSizeDelegate <NSObject>
- (void)adView:(GADBannerView *)view willChangeAdSizeTo:(GADAdSize)size;
@end
请记住在发出广告请求之前使用 setAdSizeDelegate: 设置委托。
[bannerView_ setAdSizeDelegate:self];
在释放视图之前,请务必将 GADBannerView 的 adSizeDelegate 属性设置为 nil:
- (void)dealloc {
bannerView_.adSizeDelegate = nil;
// Don't release the bannerView_ if you are using ARC in your project
[bannerView_ release];
[super dealloc];
}
我遇到了同样的问题。过去它尽可能地改变广告的 UIView 对象的框架,但现在它会立即导致didFailToReceiveAdWithError:
.
DFPBannerView setSize:
此外,使用非标准尺寸调用无效。
我的解决方案是简单地设置的缩放DFPBannerView
:
GADBannerView *retVal = [self.dfpAd];
float scaleX = w / (retVal.adSize.size.width - 0.5f);
float scaleY = h / (retVal.adSize.size.height - 0.5f);
float scaleFactor = MAX(scaleX, scaleY);
retVal.transform = CGAffineTransformMakeScale( scaleFactor, scaleFactor);
// [((DFPBannerView *) retVal) resize:GADAdSizeFromCGSize(CGSizeMake(w, h))]; // Of course not working....
// iPhone 6 and above fix. Won't rescale correctly if you don't layout. Do this after addSubView!
// [retVal setNeedsLayout];
// [retVal layoutIfNeeded];
另外 - 请注意 iPhone 6 及更高版本的修复。
在 Swift 4 中显示带有自定义框架的 adMob(模拟器中的测试代码)
class ViewController: UIViewController, GADBannerViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
print("Google Mobile Ads SDK version: \(GADRequest.sdkVersion())")
let bannerView = GADBannerView(adSize: kGADAdSizeMediumRectangle)
//OR You can give custom frame
//let bannerView = GADBannerView.init(frame:CGRect(x: 0, y: 0, width: 300, height: 250))
bannerView.adUnitID = "YOUR adUnitID"
bannerView.rootViewController = self
bannerView.delegate = self
//bannerView.adSize = kGADAdSizeMediumRectangle
let request = GADRequest()
request.testDevices = [kGADSimulatorID];
bannerView.load(request)
self.view.addSubview(bannerView)
}
//MARK : GADBannerView Delegates
/// Tells the delegate an ad request loaded an ad.
func adViewDidReceiveAd(_ bannerView: GADBannerView) {
print("adViewDidReceiveAd")
}
/// Tells the delegate an ad request failed.
func adView(_ bannerView: GADBannerView,
didFailToReceiveAdWithError error: GADRequestError) {
print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)")
}
/// Tells the delegate that a full-screen view will be presented in response
/// to the user clicking on an ad.
func adViewWillPresentScreen(_ bannerView: GADBannerView) {
print("adViewWillPresentScreen")
}
/// Tells the delegate that the full-screen view will be dismissed.
func adViewWillDismissScreen(_ bannerView: GADBannerView) {
print("adViewWillDismissScreen")
}
/// Tells the delegate that the full-screen view has been dismissed.
func adViewDidDismissScreen(_ bannerView: GADBannerView) {
print("adViewDidDismissScreen")
}
/// Tells the delegate that a user click will open another app (such as
/// the App Store), backgrounding the current app.
func adViewWillLeaveApplication(_ bannerView: GADBannerView) {
print("adViewWillLeaveApplication")
}
}
注意:在 AppDelegate
GADMobileAds.configure(withApplicationID: "YOUR ApplicationID")
根据 iOS 5 版本,kGADAdSizeBanner
只允许特定的大小更改。请参阅下面的代码。
bannerView_ = [[GADBannerView alloc]initWithFrame:CGRectMake(0.0,0.0, 320.0, 300.0)];
bannerView_.adUnitID = MY_BANNER_UNIT_ID;
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
虽然您可以查看此链接... 我的 admob 横幅显示在顶部
试试我的指示
在 YourviewController 头文件中
#import "GADBannerView.h"
@interface YourviewController : UIViewController
{
GADBannerView *admob_view;
}
在 YourViewcontroller 实现文件之后:
#import "MainViewController.h"
#import <QuartzCore/QuartzCore.h>
#define AdMob_ID @"a150349d7c43186"
@implementation YourviewController
{
-(void)viewDidLoad
{
admob_view = [[GADBannerView alloc]
initWithFrame:CGRectMake(0.0,415.0,320,60)];//in this line mention your adview size
admob_view.adUnitID = AdMob_ID;
admob_view.rootViewController = self;
[self.view addSubview:admob_view];
GADRequest *r = [[GADRequest alloc] init];
r.testing = YES;
[admob_view loadRequest:r];
}
}
上面的代码注意这一行:admob_view = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0,415.0,320,60)];
您可以使用CGRectMake(x,y,width,height)来分配修改您的 admob 广告视图,例如您在代码下面的要求:
admob_view = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0,415.0,280,50)];
您可以看到此链接https://developers.google.com/mobile-ads-sdk/docs/admob/intermediate#ios 并根据 iphone 中无法实现的 admob 广告的自定义尺寸
无需自己提供大小,只需传递要在其中添加 GADBannerView 的视图的大小。
例如:-
let size = GADAdSize(size: self.adView.size, flags: 1)
let bannerView = GADBannerView(adSize: size)
bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
bannerView.rootViewController = self
self.adView.addSubview(bannerView)
bannerView.load(GADRequest())
这是更干净和简单的方法。您将根据容器视图大小看到广告。
只需转到GADBannerView.h
文件,您将找到以下代码
#define GAD_SIZE_320x50 CGSizeMake(320, 50)
只需编辑CGSizeMake(320, 50)
为CGSizeMake(280, 50)