iAd banner position is wrong on iPhone 6/6 Plus - ios

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

Related

About iOS10 animation

I'm coding a login view, when the keyboard show, background will move the same distance with an animation, but I find a question that I can't understand, when the animation begin, at the top of the background will display a white area, the white are is just the area that will disappear when the animation finished.
Here is my code
- (void)keyboardWillShow:(NSNotification *)notification{
CGRect keyboardBounds;
[[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];
NSNumber * duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber * curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
CGRect viewFrame = self.view.frame;
viewFrame.origin.y = 0 - keyboardBounds.size.height;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
[UIView setAnimationDelegate:self];
self.bgView.frame = viewFrame;
[UIView commitAnimations];
}
Thanks in advance!

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

How to calculate keyboard height in iOS?

I know that this question is asked many times, but I can not figure out where am I making mistake. In simple words I can not implement it properly.
I have App which has to work on iPhone and iPad (in portrait orientation only). I have a subclassed UITextField. I need to move it up when it is hidden by keyboard during the edit.
I can not set it properly, because the calculation of the size of the keyboard is done after it enters in the edit mode.
I know that I need following steps:
Register observer
Have routine that calculates Keyboard rectangle from which I can take height
Have 2 events on show and hide of the keyboard to make offset of my custom text field.
Please tell me where I am wrong.
My code is:
Register observer:
- (id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillChange:) name:UIKeyboardDidShowNotification object:nil];
_leftInlineImage = NO;
}
return self;
}
Calculating keyboard height:
- (void)keyboardWillChange:(NSNotification *)notification {
NSDictionary* d = [notification userInfo];
CGRect r = [d[UIKeyboardFrameEndUserInfoKey] CGRectValue];
r = [self.superview convertRect:r fromView:nil];
_keyboardHeight = r.size.height;
}
Show/hide keyboard:
/* Move keyboard */
-(void)showKeyboardWithMove {
CGRect screenRect = [[UIScreen mainScreen] bounds];
////CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
//_keyboardHeight = KEYBOARD_HEIGHT;
if (self.frame.origin.y + self.frame.size.height > screenHeight - _keyboardHeight) {
double offset = screenHeight - _keyboardHeight - self.frame.origin.y - self.frame.size.height;
CGRect rect = CGRectMake(0, offset, self.superview.frame.size.width, self.superview.frame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
self.superview.frame = rect;
[UIView commitAnimations];
}
}
-(void)hideKeyboardWithMove {
CGRect rect = CGRectMake(0, 0, self.superview.frame.size.width, self.superview.frame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
self.superview.frame = rect;
[UIView commitAnimations];
}
The calculation is made before and my height is 0. All this is done in the subclass of the UITextField.
Add an observer of UIKeyboardWillChangeFrameNotification to the viewDidLoad;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardFrameDidChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
This method will help you :
- (void)keyboardFrameDidChange:(NSNotification *)notification
{
CGRect keyboardEndFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect keyboardBeginFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
UIViewAnimationCurve animationCurve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
NSTimeInterval animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] integerValue];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
CGRect newFrame = self.view.frame;
CGRect keyboardFrameEnd = [self.view convertRect:keyboardEndFrame toView:nil];
CGRect keyboardFrameBegin = [self.view convertRect:keyboardBeginFrame toView:nil];
newFrame.origin.y -= (keyboardFrameBegin.origin.y - keyboardFrameEnd.origin.y);
self.view.frame = newFrame;
[UIView commitAnimations];
}
that gives you back the height of the keyboard.
NSNotification * note = // ... the notification you receive via UIKeyboardWillShowNotification
id _obj = [note.userInfo valueForKey:#"UIKeyboardBoundsUserInfoKey"];
CGRect _keyboardBound = CGRectNull;
if ([_obj respondsToSelector:#selector(getValue:)]) [_obj getValue:&_keyboardBound];
the _keyboardBound will have the correct size.
NOTE: that is not a full implementation to handle the keyboard's appearance, if you need the complex idea, please check my complete and accepted answer here (stackoverflow.com internal link).

iAd positioning on iphone 5 and iphone 4 issue

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

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