How to center AdMob advertisement on iPad? - ios

I'm just testing out the AdMob code I've put into my application and I can't figure out why the advertisement is not centring on the iPad. On the iPhone it perfectly centres itself.
This is the code:
//google ad
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
googleBanner_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeLeaderboard origin:CGPointMake(0, 0)];
}else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
googleBanner_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner origin:CGPointMake(0, 0)];
}
//googleBanner_.adUnitID = #"pub";
googleBanner_.rootViewController = self;
[self.view addSubview:googleBanner_];
[googleBanner_ loadRequest:[GADRequest request]];
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
I had assumed that I could change the x-coordinate on the iPad ad position but that simply put half of it off the screen.
(this is what I did)
googleBanner_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeLeaderboard origin:CGPointMake(384, 0)]; //768/2 = 384
Any ideas as to why this is happening? What can I do to properly center the advertisement on the iPad? NOTE: kGADAdSizeLeaderboard dimensions = 728 x 90

Fixed it. For those that encounter the same problem, simply take the x-dimension of the kGADAdSizeLeaderboard which is 728, and subtract it from the total width which is 768, which gives you 40. Then divide 40 by two to have it even on both sides, giving you 20. Then simply set the x-coordinate in the origin to 20, placing the ad in the middle of the screen.
Code:
googleBanner_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeLeaderboard origin:CGPointMake(20, 0)];

Related

CustomIOS7AlertView landscape mode

I'm trying to create a CustomAlertview using CustomIOS7AlertView, with text and image. Because I read that with simple AlertView is not possible to set a maximun height when we add an image...
My problem is this: I created the CustomAlertView with the image, but I can not in any way, that for the IPAD (in landscape) it is shown in landscape mode. It is always displayed as vertical.
I've tried rotating the customAlertview, but I have not succeeded ...
Could anyone help me?
This is my code:
UIView * vista = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
UIImageView *imgView;
if (([[UIDevice currentDevice].model rangeOfString:#"iPad"].location!=NSNotFound)) {
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
} else {
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
}
[imgView setImage:img];
[vista addSubview:imgView];
CustomIOS7AlertView *alertView = [[CustomIOS7AlertView alloc] init];
[alertView setContainerView:vista];
[alertView show];
Thank you!!!
Put observers for orientation change in CustomIOS7AlertView and then make the view correctly oriented every time orientation is changed.
Refer to : Detecting iOS UIDevice orientation to understand more on orientation change notifications.
Solved! As the colleague noir_eagle said, the problem was in the show (CustomIOS7AlertView.m) method, in particular had to modify the following line, which affects the rotation:
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
switch (interfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
**self.transform = CGAffineTransformMakeRotation(M_PI * 360 / 180.0);**
Thank you ver much! ;)

Hide Admob ads view in iPad

I put an Admob banner at the bottom centre of screen using the following code:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGPoint origin = CGPointMake(0.0,
self.view.frame.size.height -
CGSizeFromGADAdSize(kGADAdSizeBanner).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
origin:origin];
bannerView_.center = CGPointMake(self.view.center.x, self.view.frame.size.height-CGSizeFromGADAdSize(kGADAdSizeBanner).height/2);
bannerView_.adUnitID = #"myid";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
}
However, in iPad, I plan to put different ads on another location. Therefore, I need to hide this ads in iPad. Is it possible for me to do this?
try this
bannerView_.hidden=true;
You do not need to make more than one banner view. The easier solution is using the same banner view but checking the current device idiom before positioning the view. For example here is the code you provided modified to check which device the user is on:
- (void) webViewDidFinishLoad: (UIWebView*) webView
{
CGPoint origin = CGPointMake(0.0,
self.view.frame.size.height -
CGSizeFromGADAdSize(kGADAdSizeBanner).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
origin:origin];
bannerView_.center = CGPointMake(self.view.center.x, self.view.frame.size.height-CGSizeFromGADAdSize(kGADAdSizeBanner).height/2);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// Change the banner's center/origin here for the iPad.
}
bannerView_.adUnitID = #"myid";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
}

Why my iAds ads corrupted on iPads

On iPhone all ok. On iPads iAds ads looks like corrupted. But when it loads new ads (blue, red etc..) all looks great. Where is my mistake?
// in applicationDidFinishLaunchingWithOptions
CGSize winSize = [[CCDirector sharedDirector] viewSize];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
adsBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, winSize.height - 66, 768, 66)];
}
else
{
adsBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, winSize.height - 50, 320, 50)];
}
// somewhere in AppDelegate
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[self.window.rootViewController.view addSubview:banner];
}
Here is image from iPad with corrupted ads:
Your question has nothing to do with iPad. Why are you initializing the ADBannerView like this? You are not using the API correctly. Use the designated initializer method initWithAdType: to get the correct result (you most likely need to use ADAdTypeBanner as the type).
To resize to fit to a specific view, you need to use what is recommended here.

Launching app in Landscape Mode

I have an app that I want to only work with in Landscape.
For the first time ever, I'm foregoing IB and trying to set up all my views programmatically, so I'm creating a view and adding a bunch of subviews in loadView method:
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.mapView.myLocationEnabled = YES;
self.mapView.delegate = self;
self.mapView.mapType = kGMSTypeHybrid;
self.mapView.frame = self.view.frame;
[self.view addSubview:self.mapView];
// add the toolbar
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);
toolbar.barStyle = UIBarStyleDefault;
NSMutableArray* items = [NSMutableArray array];
[items addObject:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"location-arrow.png"]
style:UIBarButtonItemStyleBordered
target:self
action:#selector(locateMe:)]];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"Tools"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(toolsButtonTapped:)]];
[toolbar setItems:items];
[self.view addSubview:toolbar];
In my project settings, I have disabled both portrait orientations. I also have this in my root view controller:
// Enforce Landscape Orientation
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
My problem is that the simulator starts in landscape mode, but all of the views are sized for portrait - so the bottom chunk of my views are below the screen and the right side of my screen is a big empty region.
I tried fixing this by switching the width and height of the application frame in the first line, but then that leaves some empty vertical room on the left edge of the screen for the status bar.
So, what's the correct way of doing what I'm trying to do?
Instead of using [[UIScreen mainScreen] applicationFrame]
try using [[[[[self view] window] rootViewController] view] bounds]
The bounds will represent the width and height correctly in Landscape orientation, because the bounds will take into account the transform (rotation) that has been applied, while the frame will not.
To see what I mean, set a breakpoint, and in the debugger print out the description of the top level view lldb> po [[[[self view] window] rootViewController] view]
You'll see that the view has a rotation transform and that its frame does not represent the dimensions of the screen in landscape, but represents the dimensions in portrait!
The long way to calculate the correct applicationFrame would be
BOOL iOS7 = NO;
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:#"7.0" options:NSNumericSearch] != NSOrderedAscending)
iOS7 = YES;
CGRect theFrame;
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
theFrame.origin = CGPointZero;
theFrame.size.width = screenBounds.size.height;
theFrame.size.height = screenBounds.size.width;
if (iOS7 == NO) {
// statusBarFrame will be CGRectZero if not visible, so this is safe
theFrame.size.height -= statusBarFrame.size.width; // because we're in landscape orientation
}
}
else {
theFrame = screenBounds;
if (iOS7 == NO) {
// statusBarFrame will be CGRectZero if not visible, so this is safe
theFrame.size.height -= statusBarFrame.size.height; // because we're in portrait orientation
}
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsLandscape( interfaceOrientation))
{
return YES;
}
return NO;
}
Put this code Appdelegate .M.....
Put this in current viewcontroller
// ios 5
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsLandscape( interfaceOrientation)) {
return YES;
}
return NO;
}
// ios6
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}

iOS 5 AirPlay screen not drawing correctly in landscape

I have a simple app that is targeting iPad and iOS 5+ only. The app is landscape-only. Now, I want to take advantage of AirPlay mirroring on the iPad 2.
I have followed all of Apple's examples/documentation I can find and can't get past this drawing problem. The secondary display doesn't draw in the correct orientation.
Below is the output I am seeing from a small test app. The blue box is just a simple UIView that should be taking up the entire screen, but is not. It seems to be drawing correctly in landscape, but the view is rotated 90 degrees. Notice how the blue extends past the margin on the bottom of the TV:
I think I need to somehow force the ViewController of the external window to correctly draw in landscape, but I don't know the proper way to do this. Any ideas?
Below are the relevant pieces code:
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(screenDidConnect:)
name:UIScreenDidConnectNotification
object:nil];
[self initScreens];
return YES;
}
- (void)screenDidConnect:(NSNotification *)note
{
[self initScreens];
}
- (void)initScreens
{
if ([[UIScreen screens] count] > 1)
{
UIScreen *screen = [[UIScreen screens] lastObject];
if (!self.secondaryWindow)
{
self.secondaryWindow = [[UIWindow alloc] initWithFrame:screen.bounds];
self.secondaryWindow.screen = screen;
self.secondaryWindow.hidden = NO;
}
if (!self.secondaryViewController)
{
self.secondaryViewController = [[CCKSecondaryViewController alloc] init];
}
self.secondaryWindow.rootViewController = self.secondaryViewController;
}
}
CCKSecondaryViewController.m: This is the rootViewController of the external window
- (void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
view.backgroundColor = [UIColor blueColor];
self.view = view;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = #"Secondary Screen";
[label sizeToFit];
[self.view addSubview:label];
label.center = self.view.center;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
You can find the sample app here:
http://dl.dropbox.com/u/360556/AirplayTest.zip
It's displaying in portrait on the connected screen. Having your shouldAutorotateToInterfaceOrientation method always return NO should sort it out for you.

Resources