2015-06-26 123 views
2

我有Xcode 6.3.2,並且在故事板上實現了ADBannerView。 Xcode一直顯示警告:在故事板上自動佈局實現ADBannerView

「橫幅視圖」的框架在運行時會有所不同。

我加了3個約束,你可以在下面看到它們。

所有約束 All of constraints 中心水平 Center horizontally 底部空間,底部佈局指南= 0 Bottom constraint 前導空格= 0 enter image description here

如何正確實施的旗幟?

,因爲我也用「bannerViewDidLoadAd」和「didFailToReceiveAdWithError」來調整內容,也有「bannerViewActionShouldBegin」和「bannerViewActionDidFinish」暫停並重新啓動應用程序的活動,我不能用「self.canDisplayBannerAds =真」。

回答

1

解決了!

要使用縱向和橫向上的自動佈局和尺寸類別添加iAd橫幅,但不使用canDisplayBannerAds首先聲明橫幅var bannerView: ADBannerView!

使用此設置委託,並添加橫幅,即可查看:

func loadAds() { 
    bannerView = ADBannerView(adType: ADAdType.Banner) 
    bannerView.hidden = true 
    bannerView.delegate = self 
    self.view.addSubview(bannerView) 
} 

使用下面的代碼,讓旗幟屏幕旋轉和調整屏幕內容contentView時iAd的負載(bottomConstraintcontentView到下一個約束):

override func viewDidLayoutSubviews() { 
    self.layoutAnimated(UIView.areAnimationsEnabled()) 
} 

func layoutAnimated(animated: Bool) { 
    var contentFrame = self.view.bounds 

    var sizeForBanner = bannerView.sizeThatFits(contentFrame.size) 

    var bannerFrame = bannerView.frame 
    if self.bannerView.bannerLoaded { 

     contentFrame.size.height -= sizeForBanner.height 
     bannerFrame.origin.y = contentFrame.size.height 
     bannerFrame.size.height = sizeForBanner.height 
     bannerFrame.size.width = sizeForBanner.width 

     let verticalBottomConstraint = self.bottomConstraint 
     verticalBottomConstraint.constant = sizeForBanner.height 
     self.view.layoutSubviews() 
     bannerView.hidden = false 
    } else { 
     bannerFrame.origin.y = contentFrame.size.height 
     bannerView.hidden = true 
     let verticalBottomConstraint = self.bottomConstraint 
     verticalBottomConstraint.constant = 0 
    } 
    UIView.animateWithDuration(animated ? 0.25 : 0.0, animations: { 
     self.contentView.layoutIfNeeded() 
     self.bannerView.frame = bannerFrame 
    }) 
} 

在這裏,您撥打上面的代碼顯示和隱藏橫幅廣告時加載或加載失敗的iAd

func bannerViewDidLoadAd(banner: ADBannerView!) { 
    self.layoutAnimated(true) 
} 

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) { 
    self.layoutAnimated(true) 
} 

現在您可以使用bannerViewActionShouldBeginbannerViewActionDidFinish來暫停並開始您的應用活動。 :)

0

我相信這是ADBannerView的自動佈局問題。我以類似的方式實施我的ADBannerView,我一直無法找到一種方法來消除警告,同時滿足我的ADBannerView所需的靈活性。以同樣的方式實施AdMob GADBannerView不會導致發生自動佈局警告。正如你在我的屏幕截圖中看到的那樣,ADBannerView確實會跨設備屏幕width = 600延伸,但Auto Layout仍然認爲它的默認尺寸爲width = 480

現在最好的答案是簡單地忽略警告。

ADBannerViewAutoLayout

Fullscreen

相關問題