Admob Native Advanced无法点击 [英] Admob Native Advanced Not Clickable

查看:177
本文介绍了Admob Native Advanced无法点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义视图类,该类继承自 GADNativeContentAdView 类。当我收到广告并致电代表时,我将如下所示的数据填充到自定义视图中。

I have created a custom View Class that inherits from GADNativeContentAdView Class. When I receive an advertisement and the delegate is called, I fill my custom view with the data as shown below.

一切都很好,但问题是它不可点击完全没有我试图将actionbutton userinteraction设置为false,但仍然无法正常工作。
我也尝试使用以下命令进行注册:

Everything looks fine but the problem is that it is not clickable at all. I tried to set the actionbutton userinteraction to false, but still won't work. I also tried to register using following:


-(void)registerAdView:(UIView *)adView
clickableAssetViews :(NSDictionary *)clickableAssetViews
nonclickableAssetViews:
(NSDictionary *)nonclickableAssetViews;

-(void)registerAdView:(UIView *)adView clickableAssetViews:(NSDictionary *)clickableAssetViews nonclickableAssetViews: (NSDictionary *)nonclickableAssetViews;

任何想法如何获取

- (void)setNativeContent:(GADNativeContentAd *)nativeContent
{
    self.nativeContentAd = nativeContent;
    headlineLabel.text = nativeContent.headline;
    bodyLabel.text = nativeContent.body;
    advertiserImage.image = ((GADNativeAdImage *)nativeContent.images.firstObject).image;
    [actionButton setTitle:nativeContent.callToAction forState:UIControlStateNormal];
    if (nativeContent.logo && nativeContent.logo.image)
    {
        advertiserLogo.image = nativeContent.logo.image;
    }
    else
    {
        advertiserLogo.image = advertiserImage.image;
    }
    NSDictionary *clickableArea = @{GADNativeContentHeadlineAsset:headlineLabel, GADNativeContentImageAsset:advertiserImage, GADNativeContentCallToActionAsset:actionButton};

    NSDictionary *nonClickableArea = @{GADNativeContentBodyAsset:bodyLabel};

    [nativeContent registerAdView:self clickableAssetViews:clickableArea nonclickableAssetViews:nonClickableArea];
}


推荐答案

我终于想出了办法使整个原生广告可点击,而无需使用.xib。我将GADNativeContentAdView子类化,并创建了tappableOverlay视图,该视图分配给了其父类中的未使用资产视图。在这种情况下,它就是callToActionView。然后我使用了未公开的GADNativeContentAd.registerAdView()方法:

I finally figured out a way to make the entire native ad clickable without using a .xib. I subclassed GADNativeContentAdView and created a tappableOverlay view that I assigned to an unused asset view in its superclass. In this case, it was the callToActionView. Then I used the not-so-documented GADNativeContentAd.registerAdView() method:

- (void)registerAdView:(UIView *)adView
   clickableAssetViews:(NSDictionary<GADNativeContentAdAssetID, UIView *> *)clickableAssetViews
   nonclickableAssetViews: (NSDictionary<GADNativeContentAdAssetID, UIView *> *)nonclickableAssetViews;

下面是Swift 4示例:

Here's a Swift 4 example:

class NativeContentAdView: GADNativeContentAdView  {
    var nativeAdAssets: NativeAdAssets?

    private let myImageView: UIImageView = {
        let myImageView = UIImageView()
        myImageView.translatesAutoresizingMaskIntoConstraints = false
        myImageView.contentMode = .scaleAspectFill
        myImageView.clipsToBounds = true
        return myImageView
    }()

    private let myHeadlineView: UILabel = {
        let myHeadlineView = UILabel()
        myHeadlineView.translatesAutoresizingMaskIntoConstraints = false
        myHeadlineView.numberOfLines = 0
        myHeadlineView.textColor = .black
        return myHeadlineView
    }()

    private let tappableOverlay: UIView = {
        let tappableOverlay = UIView()
        tappableOverlay.translatesAutoresizingMaskIntoConstraints = false
        tappableOverlay.isUserInteractionEnabled = true
        return tappableOverlay
    }()

    private let adAttribution: UILabel = {
        let adAttribution = UILabel()
        adAttribution.translatesAutoresizingMaskIntoConstraints = false
        adAttribution.text = "Ad"
        adAttribution.textColor = .white
        adAttribution.textAlignment = .center
        adAttribution.backgroundColor = UIColor(red: 1, green: 0.8, blue: 0.4, alpha: 1)
        adAttribution.font = UIFont.systemFont(ofSize: 11, weight: UIFont.Weight.semibold)
        return adAttribution
    }()

    override var nativeContentAd: GADNativeContentAd? {
        didSet {
            if let nativeContentAd = nativeContentAd, let callToActionView = callToActionView {
                nativeContentAd.register(self,
                                         clickableAssetViews: [GADNativeContentAdAssetID.callToActionAsset: callToActionView],
                                         nonclickableAssetViews: [:])
            }
        }
    }

    init() {
        super.init(frame: CGRect.zero)

        translatesAutoresizingMaskIntoConstraints = false
        backgroundColor = .white
        isUserInteractionEnabled = true
        callToActionView = tappableOverlay
        headlineView = myHeadlineView
        imageView = myImageView
    }

    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func didMoveToSuperview() {
        super.didMoveToSuperview()

        addSubview(myHeadlineView)
        addSubview(myImageView)
        addSubview(adAttribution)
        addSubview(tappableOverlay)
    }

//    override func updateConstraints() {
//          ....
//    }
}

只需确保将 tappableOverlay 固定到其超级视图边缘,以使它们的大小相同...在 updateConstraints()

Just be sure to pin the tappableOverlay to its superview edges so that they're the same size...in updateConstraints().

这篇关于Admob Native Advanced无法点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆