1

Hello I am trying to implement iAds at the bottom of my app When I change size to 3.5" screen, the iAd banner I have at the bottom of screen disappears. When I switch back to 4", the banner is back. How do I lock down the banner to display at bottom of screen regardless of screen size?

Here is my .h file

#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

this is .m file

@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;
}

Here is all of my code for iAds I really need help with this issue.Thanks

Moneeb
  • 441
  • 1
  • 4
  • 6
  • Check the [screen size](http://stackoverflow.com/q/5677716/643383) and position the ad view accordingly. – Caleb Aug 15 '14 at 16:21

3 Answers3

0

I know this is old but I had this problem recently, and I found a solution so I will post it here in case someone else stumbles upon it.

I noticed that when you change the phones for some reason the ViewController retained the size specified in the storyboard (in my case it was iPhone 6).

So my fix was in the viewDidLoad method I got the size of the main screen after it loaded, instead of the size of the actual view or frame.

Swift:

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

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

Objective-C:

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

and the corresponding output in their respective order:

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

I testing this on a iPhone5S which has a screen size of 320x568, but when asked for the view's bounds I got 375x667 which is the size of which I had made it in the storyboard.

Hope this helps someone!

-1

Add a #define to know which device uses the App :

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

Next, use this #define to modify the offset :

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

and

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

Set the right value for iPhone 4" and 3.5"

Jordan Montel
  • 8,227
  • 2
  • 35
  • 40
-1

Set the banner position at viewWillAppear where you can access the self.view.bounds.size, don't forget to add constraints or set autoResizingMask to make it "stick" to the bottom.

A-Live
  • 8,904
  • 2
  • 39
  • 74
  • Thanks for the answer I hope it will work could you elaborate more by writing the code ? – Moneeb Aug 15 '14 at 16:35
  • I could but I believe you can do it yourself, store a reference to `adView` when you create it, add `viewWillAppear` similar as you did for `viewWillDisappear` and set the ad view position there. – A-Live Aug 15 '14 at 16:37