如何让canDisplayBannerAds生成viewDidLayoutSubview回调?

时间:2014-02-06 21:49:54

标签: ios7 uiviewcontroller iad viewdidlayoutsubviews

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

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

1 个答案:

答案 0 :(得分:2)

我解决这个问题的方法是在设置canDisplayBannerAds = YES之前用替换layoutSubviews的UIView替换self.view

@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 -