应用程序底部的iAd横幅为3.5和4英寸屏幕

时间:2014-08-15 16:00:46

标签: ios iphone xcode iad

您好我正在尝试在我的应用程序底部实现iAds当我将大小更改为3.5“屏幕时,我在屏幕底部的iAd横幅消失。当我切换回4”时,横幅又回来了。无论屏幕大小如何,如何锁定横幅以显示在屏幕底部?

这是我的.h文件

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>

@interface ViewController : UIViewController <ADBannerViewDelegate>

{

ADBannerView *adView;
BOOL bannerIsVisible;

NSInteger HighScoreNumber;
IBOutlet UILabel *HighScore;

}

@property (nonatomic,assign) BOOL bannerIsVisible;

@end

这是.m文件

@synthesize bannerIsVisible;

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible) {

    [UIView beginAnimations:@"animatedAdBannerOn" context:NULL];
    //banner is invisible
    banner.frame = CGRectOffset(banner.frame, 0, 560);
    [UIView commitAnimations];
    self.bannerIsVisible = YES;
    [banner setAlpha:1];

}

}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible) {

    [UIView beginAnimations:@"animatedAdBannerOff" context:NULL];
    //banner is visible and we moved it out of the screen due to connection issue
    banner.frame = CGRectOffset(banner.frame, 0, -560);
    [UIView commitAnimations];
    self.bannerIsVisible = NO;

   }

}


- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:        (BOOL)willLeave
{
NSLog(@"Banner view is beginning an ad action");
BOOL shouldExecuteAction = YES;
if (!willLeave && shouldExecuteAction) {
    // stop all interactive proccess in the app
    // video pause
    //audio pause
}
return shouldExecuteAction;

}

- (void)bannerViewActionDidFinish:(ADBannerView *)banner

{

// resume everything
// video
// audio  
}

- (void)viewDidLoad
{
 [super viewDidLoad];
// Do any additional setup after loading the view.

HighScoreNumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
HighScore.text = [NSString stringWithFormat:@"HighScore: %li", (long)HighScoreNumber];


adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, -50);
[adView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[self.view addSubview:adView];
adView.delegate=self;
self.bannerIsVisible=NO;
[super viewDidLoad];

}

- (void) viewWillDisappear:(BOOL)animated
{
[adView removeFromSuperview];
adView.delegate = nil;
adView = nil;
}

以下是iAds的所有代码我真的需要帮助解决这个问题。谢谢

3 个答案:

答案 0 :(得分:0)

我知道这已经过时了,但我最近遇到了这个问题,我找到了解决方案,所以我会在这里发布,以防其他人偶然发现它。

我注意到,当您因某种原因更换手机时ViewController保留了故事板中指定的大小(在我的情况下是iPhone 6)。

所以我的修复是在viewDidLoad方法中,我在加载后获得了主屏幕的大小,而不是实际视图或框架的大小。

夫特:

var screenSize = UIScreen.mainScreen().bounds
println(screenSize)

var viewSize = self.view.bounds
println(viewSize)

目标-C:

CGSize sizeOfScreen = [[UIScreen mainScreen] bounds].size;
CGSize sizeOfView = self.view.bounds.size;

以及各自顺序的相应输出:

(0.0, 0.0, 320.0, 568.0) (0.0, 0.0, 375.0, 667.0)

我在屏幕尺寸为320x568的iPhone5S上进行测试,但当被问到视图界限时,我得到了375x667,这是我在故事板中制作的尺寸。

希望这有助于某人!

答案 1 :(得分:-1)

添加#define以了解哪个设备使用了该应用:

#define IS_IPHONE5() ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) && [UIScreen mainScreen].bounds.size.height == 568)

接下来,使用此#define修改偏移量:

banner.frame = CGRectOffset(banner.frame, 0, (IS_IPHONE5() ? 560.0f : 420.0f));

banner.frame = CGRectOffset(banner.frame, 0, (IS_IPHONE5() ? -560.0f : -420.0f));

为iPhone 4和#34设置正确的值;和3.5&#34;

答案 2 :(得分:-1)

将横幅位置设置为viewWillAppear,您可以访问self.view.bounds.size,不要忘记添加约束或设置autoResizingMask以使其成为&#34;坚持&#34;到底。