0

目前我使用它来编写加载 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似乎不起作用。

4

4 回答 4

3

我可能理解错了,但是如果您设置以下内容:

[adView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];

这应该意味着随着您的视图变大/变小(随着您旋转),它应该增加/减少“顶部”边距,因此应该坚持到底。

不过,我可能对此理解有误。如果上述想法不起作用,您能否提供当前正在发生的事情的屏幕截图?

于 2013-02-15T16:38:09.030 回答
0

viewDidLoad在你的方法中试试这个:

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

无论设备类型如何,它都会将您的 iAd 设置在视图的底部。不要在委托方法中设置 iAd 的框架。

于 2013-02-12T09:09:56.907 回答
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 语句来监听变化,并在方向变化时重新定位 Ad Banner View:

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;
}
于 2013-02-23T07:30:18.500 回答
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,马克天

于 2014-03-01T09:55:12.097 回答