iphone - 在 iPad 中的 iOs 7 iPhone 应用程序中实现 iAd 的正确方法

标签 iphone ipad ios7

我遇到了一个问题,即我的 iAd 在 iPad(模拟器或真实设备)上的横幅 View 和(如果单击)iAd View 中显示时都会被放大。它在 iPhone 上运行良好,但在 iPad 上则不行(自动缩放时)。 enter image description here

iPhone 应用程序本身非常简单,布局合理,并且仅支持纵向。

我知道 currentContentSizeIdentifier 已被弃用,但在 iOS 7 后的世界中如何处理这个问题?我尝试过使用

self.canDisplayBannerAds=YES:

...但还没弄清楚使用此方法时如何设置委托(delegate)。

这是我的 viewDidLoad 的开头,我在其中添加 iAd 横幅并将其放置在 View 的底部。

 - (void)viewDidLoad
{
    [super viewDidLoad];
  //add iAd banner
    CGRect myView=self.view.frame;  //get the view frame
  //offset the banner to place it at bottom of view
    CGRect bannerFrame= CGRectOffset(myView, 0, myView.size.height);
  //Create the bannerView
    ADBannerView *adView = [[ADBannerView alloc] initWithFrame:bannerFrame];
  //yes, deprecated, but what to use now?
    adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    [self.view addSubview:adView];
    adView.delegate = self;
...

我一如既往地欣赏你在这件事上的智慧......

最佳答案

自 iOS 6 起,Apple 决定让所有 iAd 适应整个屏幕宽度。所以你别无选择再改变它。在您的 viewDidLoad 中使用:

// On iOS 6 ADBannerView introduces a new initializer, use it when available

if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)])
{
    _bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
} else {
    _bannerView = [[ADBannerView alloc] init];
    _bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
}
_bannerView.delegate = self;
[self.view addSubview:_bannerView];

要调整 View 大小并为横幅提供正确的位置,请使用:

- (void)viewDidLayoutSubviews {
   CGRect contentFrame = self.view.bounds, bannerFrame = CGRectZero;

   // All we need to do is ask the banner for a size that fits into the layout area we are using.
   // At this point in this method contentFrame=self.view.bounds, so we'll use that size for the layout.
   bannerFrame.size = [_bannerView sizeThatFits:contentFrame.size];

   if (_bannerView.bannerLoaded) {
      contentFrame.size.height -= bannerFrame.size.height;
      bannerFrame.origin.y = contentFrame.size.height;
   } else {
      bannerFrame.origin.y = contentFrame.size.height;
   }
   _contentView.frame = contentFrame;
   _bannerView.frame = bannerFrame;
}

关于iphone - 在 iPad 中的 iOs 7 iPhone 应用程序中实现 iAd 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20954454/

相关文章:

iphone - UILocalNotification 执行操作

ipad - iPad 上的 iOS 5 UISplitViewController 不在纵向上显示 MasterView(但仅在最初)

objective-c - iOS 7 : Removing border from a section of grouped-style UITableView

ios - 有没有办法在 Xcode 8 beta 上获得 iOS 7 模拟器

cocoa-touch - 自定义 UIControl 与 UILabel 在色调颜色变化时变暗

iphone - 从 CGImageRef 创建的 UIImage 失败并显示 UIImagePNGRepresentation

iphone - heightForRowAtIndexPath : was executed before cell is show?

iphone - 如何从委托(delegate)外部设置 super View ?

ipad - UITabBarController 中的 UIViewController 和 UISplitViewController shouldAutorotateToInterfaceOrientation

android - 如何在移动浏览器中播放 swiffy HTML5 文件中的音频?