ios7 - 如何让 canDisplayBannerAds 产生 viewDidLayoutSubview 回调?

标签 ios7 uiviewcontroller iad viewdidlayoutsubviews

在我的代码中,当我在 View Controller 上设置 canDisplayBannerAds=YES 时,我会在广告消失时收到对 viewDidLayoutSubviews 的回调,但在广告出现时则不会。我猜测这是因为当您将 canDisplayBannerAds 设置为 YES 时,Apple 将 View Controller 的原始 self.view 移动到 self.originalContentView

我的问题是,对此的合理解决方法是什么?

最佳答案

我对此问题的解决方案是在设置 canDisplayBannerAds=YES 之前将 self.view 替换为覆盖layoutSubviews 的 UIView。

@protocol LayoutViewDelegate <NSObject>
- (void) layout;
@end

@interface LayoutView : UIView
@property (nonatomic, weak) id<LayoutViewDelegate> delegate;
@end
@implementation LayoutView
- (void) layoutSubviews {
    [super layoutSubviews];
    if (self.delegate) [self.delegate layout];
}
@end

我在 viewDidLoad 中进行此替换:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"Calendar.viewDidLoad");

    // Replace the view, before setting up ads for iOS7, so we can get callbacks for viewDidLayoutSubviews; otherwise, we only get viewDidLayoutSubviews callbacks when ad disappears.
    if ([Utilities ios7OrLater]) {
        self.layoutView = [[LayoutView alloc] initWithFrame:self.view.frame];
        self.view = self.layoutView;
        self.layoutView.delegate = self;
    }
}

在 viewDidAppear 中,我这样做:

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if ([Utilities ios7OrLater]) {
        self.canDisplayBannerAds = YES;
    }
}

我添加了委托(delegate)方法:

// This *always* gets called when the banner ad appears or disappears.
#pragma - LayoutViewDelegate method
- (void) layout {
   // do useful stuff here
}
#pragma -

关于ios7 - 如何让 canDisplayBannerAds 产生 viewDidLayoutSubview 回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21614801/

相关文章:

ios - MKPolyline 仅绘图点

ios - 识别屏幕锁定和解锁 ios 的滑动模式

ios - CoreMotion API 和运动许可

ios - UIPageViewController (ScrollStyle) 不调用 dataSource 方法

ios - 关于 iAd 的问题?

ios - 在 ReactiveCocoa 中订阅信号进行网络调用的正确替代方法是什么?

ios - 从 StoryBoard 加载 View Controller 时显示空白 View

iphone - 如何将 CornerRadius 赋予 UIViewController 对象 iPhone

ios - iAd 的一些问题

objective-c - 如何正确实现iAds? iOS6 弃用问题