2014-03-01 57 views
1

我試圖得到一個ADBannerView的呈現寬度,但它似乎總是同我UIScreen的mainScreen的寬度:如何在iOS 7中獲得ADBannerView的渲染寬度?

adBannerView = [[ADBannerView alloc] init]; 
[self.navController.view addSubview:adBannerView]; 
NSLog(@"Banner's width: %f.", adBannerView.frame.size.width); 
NSLog(@"Screen's width: %f.", [UIScreen mainScreen].bounds.size.width); 

兩個日誌上面顯示相同的值。我想最終水平居中我的旗幟使用下面的代碼,但寬度我從旗幟的框架背部必須呈現寬度:

adBannerView.frame = CGRectOffset(adBannerView.frame, ([UIScreen mainScreen].bounds.size.width - adBannerView.frame.size.width)/2.0f, 0); 

那麼,如何獲得ADBannerView的呈現寬度?

回答

0

只是想分享的是爲我工作的解決方案。看起來我所有的問題都是由於我使用了Cocos2D。我不應該使用[UIScreen mainScreen].bounds.size.width。這裏是我工作正常,最後,中心從我的應用程序的委託廣告(它繼承CCAppDelegate,所以它像我試圖混合和匹配兩種不同的接口尺寸的方法):

adBannerView = [[ADBannerView alloc] init]; 
adBannerView.backgroundColor = [UIColor whiteColor]; 
CGSize sizeToFit = [adBannerView sizeThatFits:[[CCDirector sharedDirector] view].frame.size]; 
// It is assumed that sizeThatFits returns size with a width smaller than the director's width, so just see if it needs to be centered. 
[adBannerView setFrame:CGRectMake(([[CCDirector sharedDirector] view].frame.size.width - sizeToFit.width)/2.0f, 0, sizeToFit.width, sizeToFit.height)]; 
adBannerView.delegate = self; 
[self.navController.view addSubview:adBannerView]; 
1

您可以使用這些尺寸bannerView:

extern NSString * const ADBannerContentSizeIdentifier320x50; 
extern NSString * const ADBannerContentSizeIdentifier480x32; 
extern NSString * const ADBannerContentSizeIdentifierPortrait; 
extern NSString * const ADBannerContentSizeIdentifierLandscape; 

但還有另一個範圍:

要調整橫幅視圖中,使用sizeThatFits:在橫幅上來看, 指定的界限包含橫幅視圖的視圖。 使用返回的大小調整橫幅視圖的大小。橫幅視圖將爲 ,其大小爲當前設備的正確寬度和高度,以及方向爲 。下面的代碼片段顯示了一個可能 實現:

ADBannerView *myBannerView = <#Get a banner view#>; 
UIView *myContainingView = <#Get the containing view#>; 
NSSize newBannerSize = [myBannerView sizeThatFits:myContainingView]; 
[myBannerView setBounds:newBannerSize]; 

如果你只是想中心的BannerView:

adBannerView.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - adBannerView.frame.size.width)/2,adBannerView.frame.origin.y,adBannerView.frame.size.width,adBannerView.frame.size.height); 

我希望它能幫助。

Documentation about ADBannerView is available here.

+0

無,這是行不通的。設置框架不會正確居中。廣告進入時,它會佔據特定的大小。這正是我想用CGRectOffset做的上面所描述的,但是描述了它不起作用。此外,使用硬編碼大小已被棄用,這就是爲什麼我針對iOS 7的這個問題,所以我不想走在使用該選項的道路上。另外,setBounds需要一個CGRect,而不是NSSize。 – Alexandru

+0

此外,即使使用sizeThatFits也會返回與[UIScreen mainScreen] .bounds.size.width相同的值。因此,回到試圖再次居中的問題上,因爲顯示的廣告不佔用整個屏幕的寬度! – Alexandru

+0

看起來,它一直不適合我的原因是因爲我的應用程序委託繼承了CCAppDelegate,因爲我使用的是Cocos2D,而且我沒有將我的應用程序寬度與導演寬度進行比較。 – Alexandru