Alexander Scholz
Alexander Scholz

Reputation: 2100

Screen size minus advertising size

How can I get the screen height minus the height of the ads I'm displaying?

I get my screen size with

 [[UIScreen mainScreen] bounds].size;

And I'm showing one banner ad:

self.canDisplayBannerAds = YES;

So the screen height should be something like:

[[UIScreen mainScreen] bounds].size.height - banner.height;

I want to place a object in the exact middle of the screen and the banner isn't showing all the time.

Upvotes: 1

Views: 126

Answers (2)

datinc
datinc

Reputation: 3552

Getting the size of the screen isn't really what you want to do. This is because if the device turns sideways (if you are supporting rotation) or if your view controller does not take up the entire screen (like if your view controller is in a UINavigationController or UITabBarController) your banner will be off the screen.

To place the banner at the bottom of the viewControllers view and place an other view in the middle of a viewControllers view you could write something like this

-(void) viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    CGRect bounds = self.view.bounds; // for a normal view controller {0,0,width,height}
    CGRect bannerRect = self.banner.frame;
    bannerRect.origin.y = bounds.size.height - bannerRect.size.height; // bottom of the screen
    bannerRect.origin.x = (bounds.size.width - bannerRect.size.width)/2.0; // centre horizontally
    self.banner.frame = bannerRect;

    CGRect objectRect = self.objectView.frame;
    objectRect.origin.y = (bounds.size.height - objectRect.size.height)/2.0; // centre vertically
    objectRect.origin.x = (bounds.size.width - objectRect.size.width)/2.0; // centre horizontally
    self.objectView.frame = objectRect;
}

Upvotes: 0

Hakim
Hakim

Reputation: 1314

canDisplayBannerAds will show an ad that you can't adopt its delegate what so ever. Also you cant reference the ad view to get frame height or width. I suggest using old fashioned way. I can post a code if you want.

Upvotes: 2

Related Questions