Swift: Unexpected zoom in ad

98 views Asked by At

I added the MoPub banner ad with the following code:

 self.adView.delegate = self
    self.adView.frame = CGRectMake(0, self.view.bounds.size.height - MOPUB_BANNER_SIZE.height,
        screenWidth, MOPUB_BANNER_SIZE.height)
    self.view.addSubview(self.adView)
    self.adView.loadAd()

For some reason when I run the application the screen doesn't appear fully, but it has a weird zoom. What could be happening?

enter image description here

UPDATE:

I moved the frame of the adView to viewDidLayoutSubviews() like this:

override func viewDidLoad(){
 self.adView.delegate = self
    self.view.addSubview(self.adView)
    self.adView.loadAd()
}


  override func viewDidLayoutSubviews() {

    var screenSize: CGRect = UIScreen.mainScreen().bounds
    var screenWidth = screenSize.width

    self.adView.frame = CGRectMake(0, self.view.bounds.size.height - MOPUB_BANNER_SIZE.height,
        MOPUB_BANNER_SIZE.width, MOPUB_BANNER_SIZE.height)

}

But the error persists, I'm using Auto Layout and Size Classes.

2

There are 2 answers

0
Omar Dlhz On BEST ANSWER

I fixed this issue by myself. When working with Spritekit and scenes, remember to always configure the scene before presenting it, specially the scene's ScaleMode. After setting the scene's scaleMode to: scaleMode= .Fill the zoom stopped happening.

5
luk2302 On

The frame properties that are present in viewDidLoad do not reflect the actual frame properties of the view that will be presented on screen. In this method the outlets have been created and all that, but not layed out yet.

Therefore it is the correct place to create an AdView, setting its properties...

BUT the layout has to be done somewhere else - the best place is viewDidLayoutSubviews. If this method is called you can be sure that the frame properties are set correctly.

Before changing anything in your code you can just log the frame in viewDidLoad and log them in viewDidLayoutSubviews - they should be different. Then you can go ahead and move your layout logic over there as well.