2013-02-10 83 views
0

目前我使用此代码来加载iAD横幅。iAD底部视图,当改变方向

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    adView = [[ADBannerView alloc] initWithFrame:CGRectZero]; 
    CGRect adFrame = adView.frame; 
    adFrame.origin.y = self.view.frame.size.height; 
    adView.frame = adFrame; 

    [adView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 

    [self.view addSubview:adView]; 
    adView.delegate=self; 

    self.bannerIsVisible=NO; 

} 

- (void)bannerViewDidLoadAd:(ADBannerView *)banner 
{ 
    if (!self.bannerIsVisible) 
    { 
     [UIView beginAnimations:@"animateAdBannerOn" context:NULL]; 

     CGRect adFrame = adView.frame; 
     adFrame.origin.y = self.view.frame.size.height-adView.frame.size.height; 
     adView.frame = adFrame; 

     [UIView commitAnimations]; 
     self.bannerIsVisible = YES; 
    } 
} 

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error 
{ 
    if (self.bannerIsVisible) 
    { 
     [UIView beginAnimations:@"animateAdBannerOff" context:NULL]; 

     CGRect adFrame = adView.frame; 
     adFrame.origin.y = self.view.frame.size.height+adView.frame.size.height; 
     adView.frame = adFrame; 

     [UIView commitAnimations]; 
     self.bannerIsVisible = NO; 
    } 
} 

问题是我想在设备方向更改时自动将iAD视图设置到视图的底部。我尝试了很多东西,但没有一个是正在工作的。

另外为什么我必须使用通知中心来检查设备方向? DidRotateToInterfaceOrientation似乎不起作用。

+0

在iOS 6中有改变旋转行为,看到这里例如http://stackoverflow.com/questions/12536645/rotation-behaving-differently-on-ios6。当界面旋转时,再次设置adbanner框架。 – 2013-02-18 11:39:58

回答

3

我可以理解这一点错,但如果你设置如下:

[adView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin]; 

这应该意味着,随着您的看法变大/小(当你旋转)应该增加/减少“顶”保证金,因此应该坚持到底。

虽然我可能已经理解错误,但是..如果上述想法不起作用,您是否能够提供当前正在发生的事情的屏幕截图?

0

尝试这在viewDidLoad方法:

CGRect adFrame = adView.frame; 
adFrame.origin.y = self.view.frame.size.height-adView.frame.size.height; 
adView.frame = adFrame; 

它将您的iAd设置为视图的底部,而不管装置的类型。不要在代理方法中设置iAd的框架。

+1

我在bannerViewDidLoadAd中有。 – 2013-02-12 09:12:10

+0

编辑我的答案。请再检查一次。 – 2013-02-12 09:21:29

+0

这似乎没有任何不同。 – 2013-02-12 12:40:52

0

该解决方案适用于iOS 6

使用通知监听在视图控制器的viewDidLoad方法的设备方向的变化:

UIDevice *device = [UIDevice currentDevice]; 
[device beginGeneratingDeviceOrientationNotifications]; 
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 
[nc addObserver:self selector:@selector(orientationChanged:)name:UIDeviceOrientationDidChangeNotification object:device]; 

UIInterfaceOrientation destOrientation = self.interfaceOrientation; 

然后在你的viewController监听使用if语句的变化,当变化方向重新定位广告横幅查看:

if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) { 

    ADBannerView *bannerView = (ADBannerView *)[self.view viewWithTag:615]; 
    CGRect myAdBanner = bnrAd.frame; 
    myAdBanner.origin.x = 0; 
    myAdBanner.origin.y = 410; 
    bannerView.frame = myAdBanner; 

} else { 

    ADBannerView *bannerView = (ADBannerView *)[self.view viewWithTag:615]; 
    CGRect myAdBanner = bnrAd.frame; 
    myAdBanner.origin.x = 0; 
    myAdBanner.origin.y = 265; 
    bannerView.frame = myAdBanner; 
} 

不要忘记调用方法来加载广告到你的观点:

- (void) bannerViewDidLoadAd:(ADBannerView *)banner { 
    bnrAd = _bannerView; 
} 

- (BOOL) bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave { 
    return YES; 
} 
0

看到下面的代码,并且需要支持iOS 6以上,我用我的iPad应用程序的代码:

在.h文件中

#import <iAd/iAd.h> 

@interface MyViewController : UIViewController <ADBannerViewDelegate> { 

    ADBannerView *bannerView; 

    BOOL bannerIsVisible; 

} 

在.m文件

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // detect device orientation change 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

    if (SYSTEM_VERSION_LESS_THAN(@"7.0")) { 

     [[NSNotificationCenter defaultCenter] 
     addObserver:self selector:@selector(orientationChanged:) 
     name:UIDeviceOrientationDidChangeNotification 
     object:[UIDevice currentDevice]]; 

    } 

    [self showiAds]; 

} 

- (void) showiAds{ 

    bannerView = [[ADBannerView alloc] initWithFrame:CGRectZero]; 

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) { 

     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

     if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) { 

      [bannerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleBottomMargin]; 

     } else { 

      [bannerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin]; 

     } 

    } 

    if (SYSTEM_VERSION_LESS_THAN(@"7.0")) { 

     [bannerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleBottomMargin]; 

    } 

    bannerView.delegate = self; 

    bannerView.hidden = YES; 

    [self.view addSubview:bannerView]; 

    bannerIsVisible = NO; 

} 

- (void)bannerViewDidLoadAd:(ADBannerView *)banner 
{ 
    NSLog(@"bannerViewDidLoadAd"); 

    if (!bannerIsVisible) { 

     bannerView.hidden = NO; 

     [UIView beginAnimations:@"animateAdBannerOn" context:NULL]; 

     // banner is invisible now and moved out of the screen on 50 px 
     banner.frame = CGRectOffset(banner.frame, 0, self.view.frame.size.height - bannerView.frame.size.height); 

     [UIView commitAnimations]; 

     bannerIsVisible = YES; 

    } 
} 

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error 
{ 
    NSLog(@"didFailToReceiveAdWithError : %@", [error localizedDescription]); 

    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; 

    } 

} 

希望你能理解上面的代码。祝你好运

Cheerio, 马克添