2

在我的代码中,当我canDisplayBannerAds=YES在视图控制器上进行设置时,我会viewDidLayoutSubviews在广告消失时收到回调,但在广告出现时不会收到回调。我猜这是因为self.originalContentView当您设置canDisplayBannerAdsYES时,Apple 将视图控制器的原始 self.view 移动到了。

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

4

1 回答 1

2

我对这个问题的解决方案是在设置 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;
    }
}

我添加了委托方法:

// This *always* gets called when the banner ad appears or disappears.
#pragma - LayoutViewDelegate method
- (void) layout {
   // do useful stuff here
}
#pragma -
于 2014-02-06T21:49:53.507 回答