iAd positioning on iphone 5 and iphone 4 issue - ios

I have a spritekit project where I want iAd to appear at the bottom of the screen. I used the storyboard to position it at the bottom of the screen. The only problem is if I put it at the bottom of an iPhone 4 screen it ends up in the middle of the iPhone 5, and if I put it at the bottom of the iPhone 5 screen it doesn't show up on the 4. I fixed this problem for a while by using banner.Center but when I change views the banner doesn't reload so it doesn't appear on the iPhone 4 screen.
#pragma mark iAd Delegate Methods
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
NSLog(#"SUCCESS");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
if (device == 4) {
banner.center = CGPointMake(160, 455);
}
if (device == 5) {
banner.center = CGPointMake(160, 543);
}
adIsThere = YES;
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
NSLog(#"ERROR");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
adIsThere = NO;
}
I found this link but when i click on the ad on the storyboard and on the size inspector, the autosizing part doesn't show up.... I think it has to do with the fact that its a SpriteKit project.
iad iPhone 5 Storyboard implementation
If you can help, Thanks!

Maybe instead of setting the position with an exact number, set it to the bottom of the screen minus the size of your banner ad.
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
banner.center = CGPointMake(160, screenHeight - bannerView_.frame.size.height);
edit: maybe you can try to use constraints if you're using storyboard, right click drag from your iAd banner to the "view" and select "Bottom Space to Bottom Layout Guide"

Do something like this
if ([[UIScreen mainScreen] bounds].size.height==480) //iPhone 4
{
banner.center = CGPointMake(160, 455);
}
else if ([[UIScreen mainScreen] bounds].size.height==568) //iPhone 5
{
banner.center = CGPointMake(160, 543);
}

Related

iOS project works on iPhone but not in simulator

Bonjour,
My iOS project support landscape and run perfectly on my iPhone 4s 7.1.2 but not in the simulator.
Here some screenshot :
iPhone Landscape
Simulator Landscape
Both screenshots have been taken in landscape mode and the simulator does not seem to resize the board correctly
Here the code I use to change orientation.
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
[UIView animateWithDuration:duration animations:^{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
int width = [[UIScreen mainScreen] bounds].size.width - [[UIApplication sharedApplication] statusBarFrame].size.width - 10;
int height = width;
int x = 5;
int y = ([[UIScreen mainScreen] bounds].size.width / 2) - (width / 2);
[board setFrame:CGRectMake(x, y, width, height)];
}];
}
else {
[UIView animateWithDuration:duration animations:^{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[board setFrame:CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.width)];
}];
}
}
Thanks for your help :)
I haven't tried your code but I strongly believe what you see is caused by the deprecated API. Official documentation here gives details, and the new API that does what you want is viewWillTransitionToSize:withTransitionCoordinator:
Also there are some other questions on stackoverflow like this talking about solutions: willAnimateRotationToInterfaceOrientation not called on ios6/7

iAd banner position is wrong on iPhone 6/6 Plus

I'm trying to optimize my SpriteKit App for iPhone 6 and iPhone 6 Plus but the iAd banner position is wrong and I can't change it. It works great on 4" or 3.5" iPhones but on iPhone 6 and 6 Plus the banner isn't at the bottom, it is a bit above it.
Also, I've been searching for this problem in google. There were many solutions but none of them worked for me.
I'm using following code for the iAd banner in my ViewController.m file:
//iAd
#pragma mark iAd Delegate Methods
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat height = CGRectGetHeight(screen);
banner.frame = CGRectOffset(banner.frame, 0, height-banner.frame.size.height);
//Changing the Y-position doesn't change the banner's position
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
if([[NSUserDefaults standardUserDefaults] boolForKey:#"Ads_savedata"] == NO)
[banner setAlpha:0];
else
[banner setAlpha:1];
[banner updateConstraints];
[UIView commitAnimations];
}
- (void) bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
}
For me, it looks like the following line doesn't affect the banner's position.
banner.frame = CGRectOffset(banner.frame, 0, height-banner.frame.size.height);
I hope someone can help me with that problem. Thanks for your attention.
I guess its screen width and height problem. Try this once
float SW;
float SH;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (( [[[UIDevice currentDevice] systemVersion] floatValue]<8) && UIInterfaceOrientationIsLandscape(orientation))
{
SW = [[UIScreen mainScreen] bounds].size.height;
SH = [[UIScreen mainScreen] bounds].size.width;
}
else
{
SW = [[UIScreen mainScreen] bounds].size.width;
SH = [[UIScreen mainScreen] bounds].size.height;
}

iAd covers my view on the first load

I have iAds working almost perfectly using a modified version of UITabBarController-iAds. The only issue I have is on the very first load of an advert my view gets covered. Once the ad refreshes after 30 seconds the view resizes just fine.
I'm using the following code:
- (void)layoutBanner
{
float height = 0.0;
ADBannerView *_banner = (ADBannerView *)[self.view viewWithTag:12];
CGRect bounds = [UIScreen mainScreen].bounds;
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
_banner.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
_banner.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
//When compiling against iOS 8 SDK this needs to be height, regardless of the actual device orientation
height = bounds.size.height;
CGSize bannerSize = [ADBannerView sizeFromBannerContentSizeIdentifier:_banner.currentContentSizeIdentifier];
//Get content view
UIView *_contentView = self.selectedViewController.view;
CGRect contentFrame = _contentView.frame;
CGRect bannerFrame = _banner.frame;
if (_banner.isBannerLoaded) {
if (iOS7) {
contentFrame.size.height = height - bannerSize.height;
bannerFrame.origin.y = contentFrame.size.height - self.tabBar.frame.size.height;
} else {
contentFrame.size.height = height - self.tabBar.frame.size.height - bannerSize.height;
bannerFrame.origin.y = contentFrame.size.height;
}
} else {
if (iOS7) {
contentFrame.size.height = height;
bannerFrame.origin.y = bounds.size.height;
} else {
contentFrame.size.height = height - self.tabBar.frame.size.height;
bannerFrame.origin.y = bounds.size.height;
}
}
[UIView animateWithDuration:0.25 animations:^{
_banner.frame = bannerFrame;
_contentView.frame = contentFrame;
} completion:^(BOOL finished) {
[self.view bringSubviewToFront:_banner];
}];
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
NSLog(#"Did load");
[self layoutBanner];
}
Any ideas?
I managed to fix this issue by playing around with the animation block. I moved the content view line and it resolved the issue
[UIView animateWithDuration:0.25 animations:^{
_banner.frame = bannerFrame;
} completion:^(BOOL finished) {
_contentView.frame = contentFrame;
[self.view bringSubviewToFront:_banner];
}];
I'm not 100% sure, but I suppose the problem is related to:
[self.view bringSubviewToFront:_banner];
The whole animation:
[UIView animateWithDuration:0.25 animations:^{
_banner.frame = bannerFrame;
_contentView.frame = contentFrame;
} completion:^(BOOL finished) {
[self.view bringSubviewToFront:_banner];
}];
is a little strange, because at the completion block you are bringing the banner view to front, so I can suppose that before animation did start - the banner was covered by some other view or views - what is the reason of frame animation if banner view is covered - thus cannot be seen while animation is in progress?
Of course the issue applies only for the first run.
as of iOs8 [[UIScreen mainScreen]bounds] has changed.. previously it would always return portrait (where H > W ) values, regardless of interfaceOrientation, but it now appears to respect interface orientation... (which is not a bad thing, unless you're taking previous behaviour for granted)
Why don't you try CGRectGetMinY(CGRect aRect) ..
eg. (Im assuming from your code the banner goes along the screen bottom, and your TAbBar across the top. (which I think is weird..) )
//once you have calculated bannerFrame
contentFrame.size.height = ( CGRectGetMinY(bannerFrame) - CGRectGetMinY (contentView.frame) );
contentFrame.origin.y = CGRectGetMaxY(self.tabbar.frame);
Do you have any constraints / resizingMasks set? when you load a second ad the view controller's view has already been resized to fit an ad, presumably the first ad is the one trying to change things..

Move CGRect to the bottom of the screen

I am running AdMob on my app, but the tutorial provided by google has the banner on top of the app, i want it at the bottom of the app/screen, i use rows 0 to 17, so anything after row 18 is fine.
This is the method that draws the ad if one was received:
-(void) adViewDidReceiveAd:(GADBannerView *)adView
{
[UIView animateWithDuration:1.0 animations:^{adView.frame = CGRectMake(0.0, 0.0, adView.frame.size.width, adView.frame.size.height);
}];
}
What would i need to do to have it displayed at the bottom of the screen?
Thank you guys.
Set the Y position to the height of the screen minus the height of the view (in landscape orientation you need to use the width of the screen instead):
adView.frame = CGRectMake(0.0, [UIScreen mainScreen].bounds.size.height.y - adView.frame.size.height, adView.frame.size.width, adView.frame.size.height);
I assume you are in the view controller:
[UIView animateWithDuration:1.0 animations:^{adView.frame =
CGRectMake(0.0,
self.view.bounds.size.height-adView.frame.size.height,
adView.frame.size.width,
adView.frame.size.height);
}];

Resize Container view sub views during animation

I'm trying to expand a Container view. But I can't figure out why the elements it contains are not resizing too... They stay with the base sizes they had.
Here's what I tried (tell me if I do something bad or if it could be done easier)
[UIView animateWithDuration:1.5
delay:0.0 usingSpringWithDamping:1
initialSpringVelocity:0
options:UIViewAnimationCurveLinear | UIViewAnimationOptionCurveLinear
animations:^{
_heightConstraint.constant = [[UIScreen mainScreen] bounds].size.height;
_widthConstraint.constant = [[UIScreen mainScreen] bounds].size.width;
_posYConstraint.constant = -20;
_posXConstaint.constant = 0;
// I tried desperately the following mess :)
for (UIView* i in _container.subviews) {
[i layoutIfNeeded];
}
[_container layoutIfNeeded];
[self.view layoutIfNeeded];
} completion:nil];
Elements i.e sub views don't transform if you re size frame. you need to change the transform of the view and it will be applied to transform of all sub views.
<your view>.transform=CGAffineTransformMakeScale(2, 2);

Resources