在我的代码中,当我在 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/