ios Facebook将FBNativeAdView添加为Subview

我想使用FBNativeAdView预建视图(不想自定义FBNative Ad)。如链接中所示

FBNativeAdView创建预建的原生广告模板视图并管理原生广告。

我在Facebook SDK中给出了Changed NativeAdSample示例。并将FBNativeAdView添加为mainView(adUIView)的子视图。

 -(void) nativeAdDidLoad: (FBNativeAd * ) nativeAd { NSLog(@"Native ad was loaded, constructing native UI..."); if (self._nativeAd) { [self._nativeAd unregisterView]; } self._nativeAd = nativeAd; // Here I did add FBNativeAdViewAttributes * attributes = [[FBNativeAdViewAttributes alloc] init]; attributes.backgroundColor = [UIColor whiteColor]; attributes.titleColor = [UIColor blackColor]; FBNativeAdView * fbNativeAdView = [FBNativeAdView nativeAdViewWithNativeAd: self._nativeAd withType: FBNativeAdViewTypeGenericHeight300 withAttributes: attributes]; } 

所以问题是如何添加fbNativeAdView作为fbNativeAdView的子视图,所以它应该在父视图中查看。我做了它

 [self.adUIView addSubview:fbNativeAdView]; 

没有成功。

原生广告模板提供了有关如何从FBNativeAd获取FBNativeAdViewFBNativeAd 。但是没有告诉他们如何在FBNativeAdView中使用FBNativeAdView。

现在它使用FBNativeAdView中的添加框架作为

 fbNativeAdView.frame = CGRectMake(0, 0, 320, 120); 

此外,现在原生广告模板提供了有关如何在FBNativeAdView中使用FBNativeAdView的信息。

可以通过更改其元素的值来自定义广告模板:

 - (void)nativeAdDidLoad:(FBNativeAd *)nativeAd { FBNativeAdViewAttributes *attributes = [[FBNativeAdViewAttributes alloc] init]; attributes.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1]; attributes.buttonColor = [UIColor colorWithRed:0.4 green:0.9 blue:0.8 alpha:1]; attributes.buttonTitleColor = [UIColor whiteColor]; FBNativeAdView *adView = [FBNativeAdView nativeAdViewWithNativeAd:nativeAd withType:FBNativeAdViewTypeGenericHeight300 withAttributes:attributes]; [self.view addSubview:adView]; CGSize size = self.view.bounds.size; CGFloat xOffset = size.width / 2 - 160; CGFloat yOffset = (size.height > size.width) ? 100 : 20; adView.frame = CGRectMake(xOffset, yOffset, 320, 300); // Register the native ad view and its view controller with the native ad instance [nativeAd registerViewForInteraction:adView withViewController:self]; } 

NativeAdView使用FBMediaView创建广告。 您对nativeAdView 300的高度太低,无法加载任何类型的FBMediaView。

如果要使用300的高度,请创建本机视图(iOS)并使用fbNativeAd返回的属性。 例如,在nativeAdDidLoad中执行此操作:

 customView.titleLabel = nativeAd.title; FFBAdImage *icon = nativeAd.icon; [icon loadImageAsyncWithBlock:^(UIImage * _Nullable image) { [customView.imageView setImage:image]; //image returned by fb is 128x128 }]; customView.fbAdBtn.titleLabel.text = nativeAd.callToAction; [self.view addSubView:customView]; 

如果您希望整个自定义视图可以点击,那么就可以了

 [nativeAd registerViewForInteraction:customView withViewController:self]; 

如果您只想通过自定义视图中的按钮执行操作,请执行此操作。

 NSArray *clikAble = [NSArray alloc] initWithObjects:customView.fbAdBtn]; [nativeAd registerViewForInteraction:customView withViewController:self withClickableViews:clikAble]; 

根据您的需要,您可以使用许多其他属性。

并且不要忘记遵循以下准则: https : //developers.facebook.com/docs/audience-network/guidelines/native-ads

并尝试使全屏大小FBNativeAdView我认为肯定会加载一个视图。