How to integrate AdBannerView in Cocos2d

I am trying to integrate a new ADBannerView object into my Cocos2d game, but the banner appears vertically to the left of the screen when my game is in landscape. This is my code:

UIViewController *controller = [[UIViewController alloc] init];
controller.view.frame = CGRectMake(0,0,480,32);

//From the official iAd programming guide
ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectZero];

adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier480x32];

adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;

[controller.view addSubview:adView];

//Then I add the adView to the openglview of cocos2d
[[Director sharedDirector] openGLView] addSubview:controller.view];

I want the banner to appear at the top of the screen in horizontal (landscape mode).

Thank you for your support!

+3
source share
2 answers

You will need to rotate the frame that you created. Try something like this:

// lower right:-136, 295, 320, 32    lower left:-136, 135, 320, 32   upper right:136, 295, 320, 32
UIViewController *controller = [[UIViewController alloc] init];
controller.view.frame = CGRectMake(136, 135, 320, 32);
controller.view.transform = CGAffineTransformMakeRotation(M_PI / 2.0); // turn 180 degrees

//From the official iAd programming guide
ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectZero];

adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier480x32];

adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;

[controller.view addSubview:adView];

//Then I add the adView to the openglview of cocos2d
[[Director sharedDirector] openGLView] addSubview:controller.view];

M_PI is defined in math.h in the cocos2d library, it's just pi. Once you get it, just play with the first two numbers in the rectangle to place it where you need it.

+1
source

shouldAutorotateToInterfaceOrientation.

0

Source: https://habr.com/ru/post/1749449/


All Articles