我有一个要添加 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/