Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获得ADBannerView';iOS 7中的渲染宽度是多少?_Ios_Objective C_Cocos2d Iphone_Iad_Adbannerview - Fatal编程技术网

如何获得ADBannerView';iOS 7中的渲染宽度是多少?

如何获得ADBannerView';iOS 7中的渲染宽度是多少?,ios,objective-c,cocos2d-iphone,iad,adbannerview,Ios,Objective C,Cocos2d Iphone,Iad,Adbannerview,我试图获取ADBannerView的渲染宽度,但它似乎总是与我的UI屏幕的主屏幕宽度相同: adBannerView = [[ADBannerView alloc] init]; [self.navController.view addSubview:adBannerView]; NSLog(@"Banner's width: %f.", adBannerView.frame.size.width); NSLog(@"Screen's width: %f.", [UIScreen mainScr

我试图获取ADBannerView的渲染宽度,但它似乎总是与我的UI屏幕的主屏幕宽度相同:

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的渲染宽度呢?

您可以将这些尺寸用于bannerView:

extern NSString * const ADBannerContentSizeIdentifier320x50;
extern NSString * const ADBannerContentSizeIdentifier480x32;
extern NSString * const ADBannerContentSizeIdentifierPortrait;
extern NSString * const ADBannerContentSizeIdentifierLandscape;
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);
但还有另一个范围:

要调整横幅视图的大小,请在横幅视图上使用sizeThatFits:, 指定包含横幅视图的视图的边界。 使用返回的大小调整横幅视图的大小。将显示横幅视图 调整到当前设备的正确宽度和高度,以及 方向。下面的代码片段显示了一个可能的 实施:

我希望有帮助


只是想分享一个对我有用的解决方案。看起来我所有的问题都是因为我使用了Cocos2D。我不应该使用
[UIScreen mainScreen].bounds.size.width
。以下是我成功地将应用程序的委托(它继承了CCAppDelegate,因此我试图混合和匹配两种不同的界面大小调整方法)中的广告放在中心位置的方法:


不,这行不通。设置框架不会使其正确居中。当广告出现时,它会呈现特定的大小。这正是我在上面试图用cRectOffset做的,但描述了它是如何不起作用的。另外,使用硬编码的大小是不推荐的,这就是为什么我针对iOS 7提出这个问题,所以我真的不想使用这个选项。此外,setBounds采用CGRect,而不是NSSize。此外,即使使用sizeThatFits也会返回与[UIScreen mainScreen].bounds.size.width相同的值。因此,回到试图再次将其居中的问题,因为显示的广告不会占据整个屏幕的宽度!看起来它一直对我不起作用的原因是因为我的应用程序代理继承了CCAppDelegate,因为我使用的是Cocos2D,并且我没有将我的应用程序的宽度与控制器的宽度进行比较。这在iOS 6中被弃用,但苹果没有提供替代方案!
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);
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];