0

I am integrating a Google AdMob banner ad into a UIView container that has the following constraints:

View Hierarchy

Main View (root view of UIViewController). Container View (inside Main View, used to hold adBannerView). adBannerView (added programmatically inside Container View).

UIViewController's Main View │ ├── Container View (UIView) │ ├── Ad Banner View (GADBannerView or other ad view)

Constraints Container View: Left: 0 (Aligns to the left of the main view) Right: 0 (Aligns to the right of the main view) Bottom: 0 (Sticks to the bottom of the main view) Height: 70 (Fixed height)

Ad Banner View: Same bounds as Container View (matches its frame)

  • Container View is pinned to the bottom of the screen.
  • Ad Banner View is programmatically added inside Container View and set to match its bounds.
  • When the ad is loaded, it appears inside Container View without affecting other UI elements.

Here is my code for adding the banner ad:

func showAd(bannerViewContainer: UIView, VC: UIViewController) {
    var bannerView: BannerView!
    bannerView = BannerView(adSize: AdSizeFullBanner)
    bannerView.adUnitID = bannerAdsId
    bannerView.rootViewController = VC
    
    bannerView.load(Request())
    bannerView.frame = bannerViewContainer.bounds
    bannerViewContainer.addSubview(bannerView)
}

However, when the ad appears on the screen, I notice that there is extra space on the left side of the banner. The banner is not aligned properly within the container view.

What I Have Tried:

  • Checked the constraints - The container view has proper constraints (0 on left, right, and bottom).
  • Set the banner frame explicitly - I tried manually setting bannerView.frame = bannerViewContainer.bounds, but the issue persists.
  • Tried different ad sizes - Using AdSizeBanner and AdSizeSmartBannerPortrait, but the issue remains.

Questions:

  • Why is there extra left padding in the banner ad when it's inside a container with full width?

Image of output The output I have

5
  • Have you tried using GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth from official documentation? Commented Feb 28 at 10:41
  • it's make it In centre but not fully starched as width of view maybe google ads has a specific width you can't change it much , But Thank You for your help , You help this whole Day Commented Feb 28 at 13:16
  • can you add the view hierachy to your post? I think it will help a lot Commented Feb 28 at 13:22
  • ok let me add , I add view hierachy direct into question so every one can see it Commented Mar 1 at 5:10
  • It is recommended to change to the latest version of SDK or the corresponding functions and codes. stackoverflow.com/a/79551643/17373286 Also, the Admob in iOS is unstable in providing the top, bottom, left, and right margins/padding or positions of the ad unit from Google. This also depends on the design quality of each advertiser. Commented Apr 7 at 21:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.