Search code examples
iosobjective-csprite-kitiad

Positioning objects relative to an ADBannerView


I've been trying to wrap my head around the coordinates of an iAd banner view, which apparently goes by its own laws. I have a banner view at the bottom of the screen, and I define it likewise:

    adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    CGRect adFrame = adView.frame;
    adFrame.origin.y = self.scene.view.frame.size.height - adView.frame.size.height;
    adView.frame = adFrame;
    [self.view addSubview:adView];

(this is inside of a sprite kit scene) So now, I want to position an object/node relative to this banner view. My first instinct to place this object higher than the banner by 5 was to say

node.position = CGPointMake(CGRectGetMidX(self.frame), adView.frame.size.height + 5);

However, this didn't give me any promising results so I decided to hardcode it and it turned out that I was able to get what I wanted if I set the y-value at around 64ish. I tested this value on an iPhone 6, which gave me what I wanted, but then when I tried it on the iPhone 5, it disappeared under the banner view. If the height of the iPhone adBannerView is 50 regardless of the model, this seems to be very contradictory.

Any help will be appreciated, thanks!


Solution

  • The problem is your scene is being scaled, so the positions you specify will vary across devices. You want to position your nodes relative to the view because that is where your banner is located. To do this you can convert the view coordinates to scene coordinates by using the SKView's convertPointToScene method. See my answer about positioning nodes relative to the view here for more info.

    You can also choose to not scale your scene at all by setting your scene's scaleMode to ResizeFill. This will result in your positions being consistent across devices because there will be no scaling of your coordinates. You can see my answer about scene scaling here here for more info.