In my code, when I set canDisplayBannerAds=YES on my view controller, I get callbacks to viewDidLayoutSubviews when the ad disappears but not when the ad appears. I'm guessing this is because Apple moves the original self.view of the view controller to self.originalContentView when you set canDisplayBannerAds to YES.

My question is, what is a reasonable work around for this?

有帮助吗?

解决方案

My solution to this problem is to replace self.view before setting canDisplayBannerAds=YES with a UIView that overrides layoutSubviews.

@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

I do this replacement in 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;
    }
}

And in viewDidAppear, I do:

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

And I add the delegate method:

// This *always* gets called when the banner ad appears or disappears.
#pragma - LayoutViewDelegate method
- (void) layout {
   // do useful stuff here
}
#pragma -
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top