按下按钮时隐藏 iAd 视图
Posted
技术标签:
【中文标题】按下按钮时隐藏 iAd 视图【英文标题】:Hide iAd view on pressing a button 【发布时间】:2014-11-14 11:45:20 【问题描述】:当我按下按钮时,我找不到隐藏 iAd 视图的正确解决方案。 我正在像这样加载 iAd 视图:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
完美运行。但是,如果我不想显示 iAd 视图,我该如何隐藏?
我在这里寻找答案,但没有直接的答案。
【问题讨论】:
【参考方案1】:如果用户没有互联网连接,则横幅将为空白。苹果要求在网络不可用时隐藏横幅。
考虑到你是这样创建的,
- (void)createAdBannerView
Class classAdBannerView = NSClassFromString(@"ADBannerView");
if (classAdBannerView != nil)
self.adBannerView = [[[classAdBannerView alloc]
initWithFrame:CGRectZero] autorelease];
[_adBannerView setRequiredContentSizeIdentifiers:[NSSet setWithObjects:
ADBannerContentSizeIdentifier320x50,
ADBannerContentSizeIdentifier480x32, nil]];
if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation))
[_adBannerView setCurrentContentSizeIdentifier:
ADBannerContentSizeIdentifier480x32];
else
[_adBannerView setCurrentContentSizeIdentifier:
ADBannerContentSizeIdentifier320x50];
[_adBannerView setFrame:CGRectOffset([_adBannerView frame], 0,
-[self getBannerHeight])];
[_adBannerView setDelegate:self];
[self.view addSubview:_adBannerView];
调用 [self.adBannerView removeFromSuperview];你何时何地需要隐藏。
【讨论】:
【参考方案2】:@Matiass21,这里写下面的代码 _bannerIsVisible 是 bool 值,_adBanner 是 ADBannerView 对象。
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
if (!_bannerIsVisible)
// If banner isn't part of view hierarchy, add it
if (_adBanner.superview == nil)
[self.view addSubview:_adBanner];
[UIView beginAnimations:@"animateAdBannerOn" context:NULL];
// Assumes the banner view is just off the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
[UIView commitAnimations];
_bannerIsVisible = YES;
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
NSLog(@"Failed to retrieve ad");
if (_bannerIsVisible)
[UIView beginAnimations:@"animateAdBannerOff" context:NULL];
// Assumes the banner view is placed at the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
_bannerIsVisible = NO;
【讨论】:
好的...但是当我想删除 iAd 视图时 [_adBanner removeFromSuperview] 不起作用? 试试这个代码 adBanner = nil;, adBanner.delegate = nil;和 adBanner.hidden = YES;以上是关于按下按钮时隐藏 iAd 视图的主要内容,如果未能解决你的问题,请参考以下文章