I have a strange problem. iAds work. As soon as I initialize a UIButton (using "[self initCloseAdButtonWithFrame:_frame];
") The iAds stop working, while the button is initialized in the correct place. This code slides an ad banner in and out depending on the availability of the ad.
Why is the initialization of closeButton breaking the ads functionality?
ViewController.m
#pragma mark - Ads
-(void)initiAdBanner
{
DLog(@"");
if (!self.iAdBannerView)
{
CGRect rect = CGRectMake(0, self.view.frame.size.height, 0, 0);
self.iAdBannerView = [[ADBannerView alloc]initWithFrame:rect];
self.iAdBannerView.delegate = self;
self.iAdBannerView.hidden = TRUE;
[self.view addSubview:self.iAdBannerView];
}
}
#pragma mark - ADBanner delegate methods -
// Called before the add is shown, time to move the view
- (void)bannerViewWillLoadAd:(ADBannerView *)banner
{
DLog(@"iAd load");
[self hideBanner:self.gAdBannerView];
[self showBanner:self.iAdBannerView];
}
// Called when an error occured
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
DLog(@"iAd error: %@", error);
[self hideBanner:self.iAdBannerView];
GADRequest *request = [GADRequest request];
request.testDevices = [NSArray arrayWithObjects:GAD_SIMULATOR_ID, nil];
[self.gAdBannerView loadRequest:request];
}
#pragma mark - Banner hide and show -
// Hide the banner by sliding down
-(void)hideBanner:(UIView*)banner
{
if (banner && ![banner isHidden])
{
[UIView beginAnimations:@"hideBanner" context:nil];
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
banner.hidden = TRUE;
if(closeButton){
[closeButton setHidden:TRUE];
}
}
}
// Show the banner by sliding up
-(void)showBanner:(UIView*)banner
{
if (banner && [banner isHidden])
{
CGRect _frame2 = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
CGRect _frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
[self initCloseAdButtonWithFrame:_frame];
[UIView beginAnimations:@"showBanner" context:nil];
banner.frame = _frame2;
[UIView commitAnimations];
banner.hidden = FALSE;
}
}
//button initialize
-(void)initCloseAdButtonWithFrame:(CGRect)frame{
DLog(@"");
closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeButton addTarget:self
action:@selector(inAppPurchase)
forControlEvents:UIControlEventTouchUpInside];
UIImage *_closeImage = [UIImage imageNamed:@"closeButton.png"];
UIImage *closeImage = [UIImage imageWithCGImage:_closeImage.CGImage scale:_closeImage.scale orientation:UIImageOrientationDown];
[closeButton setBackgroundImage:closeImage forState:UIControlStateNormal];
[closeButton setBackgroundImage:closeImage forState:UIControlStateHighlighted];
closeButton.frame = CGRectMake(frame.size.width - closeImage.size.width, -5 + self.view.frame.size.height -frame.size.height - closeImage.size.height, closeImage.size.width, closeImage.size.height);
[self.view insertSubview:closeButton atIndex:2];
[closeButton setHidden:FALSE];
}
-(void)inAppPurchase{
DLog(@"");
}
ShowBanner is called via a delegate of iAd. In Show banner, the button is initialized. The code works (ads show up) only if the button is not initialized.
In particular, everything works fine until the button is added to the subview.