I have added iAd to my iphone app to be at the top of my app. originally I place it at x=0 and y=-50 so that it comes from off the screen. I use the following code for it in my .m :
- (void)bannerView:(ADBannerView *)abanner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// Assumes the banner view is placed at the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
- (void)bannerViewDidLoadAd:(ADBannerView *)abanner
{
if (!self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
// Assumes the banner view is just off the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
When my app launch iAd get displayed at the top without any problem. but when I open another app and come back to it (without killing it so my app is running in the background) the banner moves another 50 pixel down
any idea?
You are adding 50.0px to banner.frame.origin.y in both cases.
Anyway: even if you'd be substracting 50.px in didFailToReceiveAdWithError: it
could happen that didFailToReceiveAdWithError: would get called multiple times in a
row and your code could move the banner higher and higher up (-50.0,-100.0,-150.0...).
So it's better to hardcode the hidden & visible positions instead of calculating it.
Try this:
- (void)bannerView:(ADBannerView *)abanner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
banner.frame = CGRectMake(0.0,-50.0,banner.frame.size.width,banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
- (void)bannerViewDidLoadAd:(ADBannerView *)abanner
{
if (!self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
banner.frame = CGRectMake(0.0,0.0,banner.frame.size.width,banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
Related
I am making the iPad app in which I want to show animation when I start click on beginEditing here is the my code.
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"This is calling");
[self showAnimationPests];
}
-(void) showAnimationPests
{
[UIView animateWithDuration:0.5
animations:^{
subView.frame = CGRectMake(0,-200,1024,748);
}];
}
It shows Log this is calling but view does not move.
If it's an iphone app why is the size of it 1024x748?
CGRectMake takes points not pixels (if you are trying to make up for retina display). And those points for iPhone 5 are 320x568 and previous devices 320x480.
try with my bellow code
Example..
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
subView.frame = CGRectMake(0,-200,1024,748);
[UIView commitAnimations];
return YES;
}
and set to 0 like default in textFieldShouldReturn: method like bellow..
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
subView.frame = CGRectMake(0,0,1024,748);
[UIView commitAnimations];
return YES;
}
My app just got rejected because "The banner within the app should be hidden whenever ad content is not being served by iAd." Then they supply this sample code;
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// assumes the banner view is at the top of the screen.
banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
Now my iAd displays at the bottom of the screen not the top. Also I wanted to take into count 3.5 vs 4 inch screens so here is my code;
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
if(result.height == 1136){
banner.frame = CGRectOffset(banner.frame, 498, -banner.frame.size.height);
}else{
banner.frame = CGRectOffset(banner.frame, 410, -banner.frame.size.height);
}
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
The really annoying part is that my codes works correctly on my test iPhone and in the iOS simulator.
What am I doing wrong?
If your banner is in the bottom, you should sum banner.frame.size.height instead of substract to hide the banner. There is no need to do extra calculations for different screens.
So, this should be enough:
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
I have IBOutlet on bottom of the screen for ADBannerView, and I won't to hide it when it shows white screen (not able to load an Ad).
I have this code, but it is not working:
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[UIView beginAnimations:#"showAd" context:nil];
CGRect adBannerViewFrame = [bannerView frame];
adBannerViewFrame.origin.x = 160;
adBannerViewFrame.origin.y = 523;
bannerView.frame = adBannerViewFrame;
[UIView commitAnimations];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
[UIView beginAnimations:#"hideAd" context:nil];
CGRect adBannerViewFrame = [bannerView frame];
adBannerViewFrame.origin.x = 0;
adBannerViewFrame.origin.y = 0;
bannerView.frame = adBannerViewFrame;
[UIView commitAnimations];
}
Can you help me please, I'm struggling with it for past two hours... :/
you should try something like this (this is from a live project). the iAd banner is at the bottom of the screen and it goes down when it becomes invisible, and it comes up back when it must be visible.
the _isiADBannerVisible is just a simple Boolean variable.
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
if (_isiADBannerVisible == false) {
_isiADBannerVisible = true;
[UIView animateWithDuration:0.5f delay:0.f options:UIViewAnimationCurveEaseInOut animations:^{
[banner setFrame:CGRectOffset(banner.frame, 0.f, -50.f)];
} completion:nil];
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
if (_isiADBannerVisible == true) {
_isiADBannerVisible = false;
[UIView animateWithDuration:0.5f delay:0.f options:UIViewAnimationCurveEaseInOut animations:^{
[banner setFrame:CGRectOffset(banner.frame, 0.f, +50.f)];
} completion:nil];
}
}
I'm adding iAd on my app.... I managed to have the banner sliding from the bottom (just above my tab bar, 0, 410 i guess)... but only when i launch the app the first time. when then i try to switch the wifi off on my testing device to check if i get the blank banner i get the following message:ADBannerView: WARNING A banner view (0x1b11d0) has an ad but may be obscured. This message is only printed once per banner view. Can please somebody help me??
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOn" context:NULL];
NSLog(#"there are ads to show");
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
//----hide banner if can't load ad.
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOff" context:NULL];
NSLog(#"sorry, no ads ");
banner.frame = CGRectOffset(banner.frame, 0, 410);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
aBanner = [[ADBannerView alloc]initWithFrame:CGRectZero];
aBanner.frame = CGRectOffset(aBanner.frame, 0, 410);
aBanner.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
aBanner.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
[self.view addSubview:aBanner];
aBanner.delegate=self;
self.bannerIsVisible=NO;
[super viewDidLoad];
I know something is wrong in the CGRectOffset but I'm not able to figure it out.
thanks
FIND THE PROBLEM.... WAS JUST A SIMPLE "!" HERE THE CODE FIXED:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOn" context:NULL];
NSLog(#"there are ads to show");
banner.frame = CGRectOffset(banner.frame,0, -banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
//----hide banner if can't load ad.
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
if (self.bannerIsVisible) { //THE PROBLEM WAS HERE!!!!
[UIView beginAnimations:#"animatedAdBannerOff" context:NULL];
NSLog(#"sorry, no ads ");
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
aBanner = [[ADBannerView alloc]initWithFrame:CGRectZero];
aBanner.frame = CGRectOffset(aBanner.frame, 0, 410);
aBanner.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
aBanner.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
[self.view addSubview:aBanner];
aBanner.delegate=self;
self.bannerIsVisible=NO;
[super viewDidLoad];
I have added a banner view for implementing iAd in the bottom of the screen. But when running on the simulator the banner view is slightly above the frame fixed for it. It appears as a transparent strip and is not selectable. After sometime it automatically comes to the bottom as a black strip saying Test Advertisement.
I want the banner View to stick to the bottom and not animate.
Here is my code. The adView declaration code:
adView = [[[ADBannerView alloc] initWithFrame:CGRectOffset(CGRectZero, 0, 350)] autorelease];
adView.frame = CGRectMake(0,340,320,25);
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
adView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin;
adView.tag = 111;
[self.navigationController.view addSubview:adView];
adView.delegate = self;
self.bannerIsVisible = NO;
adView.hidden = YES;
Here are the delegate methods.
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// banner is invisible now and moved out of the screen on 50 px
banner.frame = CGRectOffset(banner.frame, 0, 50);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
// When iAd is not availale on the banner
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible) {
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// banner is visible and we move it out of the screen, due to connection issue
banner.frame = CGRectOffset(banner.frame, 0, 520);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
I am not able to understand the problem. Kindly help.
First.
When the main view is loaded how are the objects placed in it initially? I always place them in right positions inside viewDidLoad method. So place your banner below the bottom of the main view making sure that
myBannerFrame.origin.x = 0;
myBannerFrame.origin.y = mainView.frame.size.height;
[myBanner setFrame:myBannerFrame]; // no need for animation here!
Also viewDidLoad is a good place to set your flag self.bannerIsVisible to FALSE.
Second. Do not use fixed pixel values like here
banner.frame = CGRectOffset(banner.frame, 0, 50);
instead of 50 you better put banner.frame.size.height
This will help you to avoid many errors and misalignments.