ios - UITableViewController 中的 iAd

标签 ios uitableview iad

我有一个要添加 iAd 的 UITableViewController。我希望广告始终显示在屏幕底部。我在这里遵循了答案:https://stackoverflow.com/a/9857798/2584268并实现了这种行为。但是,广告在开始时是隐藏的,只有在用户滚动表格时才会出现。如何让广告在 View 加载时显示在屏幕底部,而不仅仅是在用户滚动时显示?

最佳答案

为了在 View 出现时显示广告,我在 viewDidAppear 上添加了横幅 View 。

- (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];

我实际上在应用委托(delegate)中制作了横幅,并在所有 View Controller 中使用它。为了处理旋转,我在 viewDidLoad 中注册了一个通知。

- (void)viewDidLoad
{
[super viewDidLoad];

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

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

我唯一做的另一件事是放入此代码以将 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];
}

关于ios - UITableViewController 中的 iAd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24947068/

相关文章:

ios - Sprite Kit 不会停止添加节点

ios - UIwebView - 加载 dismissView 时留下的 URL

iphone - 如何在导航栏上添加带有动画的广告?

ios - UI 未在主线程中更新

objective-c - Core Plot 的 CPTPlotSpaceDelegate : Correct implementation of zooming and panning

ios - 如何设置 UITableViewRowAction 的 Action

ios - 在静态 TableView 单元格中实现照片库

ios - 我们可以在 UITableView 的一部分中快速实现部分吗?

IOS:在 iOS 应用程序中转换广告

swift - 从标签栏应用程序中的任何地方快速调用方法