iAd在UITableViewController中

时间:2014-07-25 02:22:04

标签: ios uitableview iad

我有一个UITableViewController,我想添加iAds。我希望广告始终显示在屏幕底部。我已经按照这里的答案:https://stackoverflow.com/a/9857798/2584268并且已经实现了这种行为。但是,广告隐藏在开头,仅在用户滚动表格时显示。如何加载视图时,如何让广告显示在屏幕底部,而不仅仅是在用户滚动时?

1 个答案:

答案 0 :(得分:1)

要在视图显示时显示广告,我会在viewDidAppear上添加横幅视图。

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

_myIAD = [[self appdelegate] myIAD];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    //iPhone code
    if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
        {
            _myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 50, 320, 50);
        }
        else
        {
            _myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 114, 320, 50);
        }
    }
    else if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
    {
        if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
        {
            _myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 32, 480, 32);
        }
        else
        {
            _myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 84, 480, 32);
        }
    }
}
else
{
    //iPad code
    if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
        {
            _myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 66, 768, 66);
        }
        else
        {
            _myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 130, 768, 66);
        }
    }
    else if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
    {
        if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
        {
            _myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 66, 1024, 66);
        }
        else
        {
            _myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 130, 1024, 66);
        }
    }
}

[self.view addSubview:_myIAD];
}

有很多代码可以处理iOS6.1,iPad和iPhone,但它的效果非常好。我总是喜欢使用statusBarOrientation来查找方向,它会更好。

如您所见,有一行

_myIAD = [[self appdelegate] myIAD];

我实际上在app delegate中制作了横幅,并在所有视图控制器中使用它。为了处理轮换,我在viewDidLoad中注册了一个通知。

- (void)viewDidLoad
{
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUI:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

基本上在updateUI方法的viewDidAppear中使用相同的代码:

我做的唯一另一件事就是输入此代码以使iAd保持在最底层。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect frame = _myIAD.frame;
frame.origin.y = _myTableView.contentOffset.y + _myTableView.frame.size.height-_myIAD.frame.size.height; // Move view to the bottom on scroll
_myIAD.frame = frame;
[_myTableView bringSubviewToFront:_myIAD];
}