I am currently integrating a UIViewController with UIViewControllerRepresentable into my SwiftUI project and would now like to change the frame of the UIViewController.
For that I used this code:
let vc = UIViewController()
vc.view.frame.size = CGSize(width: kGADAdSizeBanner.size.width, height: kGADAdSizeBanner.size.height)
However, when I integrate the view created by UIViewControllerRepresentable into my SwiftUI project in a VStack with a list, the UIViewController does not have the size I want. Look at this
My hole code is:
func makeUIViewController(context: Context) -> UIViewController {
let vc = UIViewController()
vc.view.frame.size = CGSize(width: kGADAdSizeBanner.size.width, height: kGADAdSizeBanner.size.height)
vc.view.backgroundColor = .gray
vc.view.alpha = 0
let bannerView = GADBannerView(adSize: kGADAdSizeBanner)
bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
bannerView.alpha = 0
bannerView.rootViewController = vc
bannerView.delegate = context.coordinator
bannerView.load(GADRequest())
vc.view.addSubview(bannerView)
return vc
}
kGADSizeBanner is a size variable from Google for the ad.
Also, I was wondering why my ad isn't appearing in the center of the gray UIViewController. Can someone help out?
What I tried so far:
vc.view.translatesAutoresizingMaskIntoConstraints = falsevc.view.frame = CGRect(origin: .zero, size: CGSize(width: kGADAdSizeBanner.size.width, height: kGADAdSizeBanner.size.height))