目前我用它来编码加载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 :(得分:3)
我可能已经理解了这个错误,但是如果你设置了以下内容:
[adView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
这应该意味着当你的视图变大/变小时(旋转时)它应该增加/减少'顶部'边距,因此应该坚持到底部。
我可能已经错误地理解了这一点。如果上述想法不起作用,你能提供当前正在发生的截图吗?
答案 1 :(得分:0)
在viewDidLoad
方法中尝试此操作:
CGRect adFrame = adView.frame;
adFrame.origin.y = self.view.frame.size.height-adView.frame.size.height;
adView.frame = adFrame;
无论设备类型如何,它都会将您的iAd设置为视图的底部。不要在委托方法中设置iAd的框架。
答案 2 :(得分: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;
}
答案 3 :(得分:0)
请参阅下面的代码,我在我的iPad App中使用此代码,需要支持iOS 6及更高版本:
您的.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, Mark Thien