不使用Storyboard加载Admob(Swift)

Google的指令是Storyboard方法。 如何在不使用Storyboard情况在顶部显示Admob横幅 ? 我试图把它放在这个代码中的ScrollView

我试图把这个viewDidLoad ,但它不工作。

 override func viewDidLoad() { super.viewDidLoad() var bannerView: GADBannerView = GADBannerView(adSize: kGADAdSizeBanner) bannerView.adUnitID = "ca-app-pub-MYID" bannerView.rootViewController = self bannerView.loadRequest(GADRequest()) view.addSubview(bannerView) mainScrollView = UIScrollView(frame: self.view.bounds) mainScrollView.pagingEnabled = true mainScrollView.showsHorizontalScrollIndicator = false mainScrollView.showsVerticalScrollIndicator = false pageScrollViews = [UIScrollView?](count: photos.count, repeatedValue: nil) let innerScrollFrame = mainScrollView.bounds mainScrollView.contentSize = CGSizeMake(innerScrollFrame.origin.x + innerScrollFrame.size.width, mainScrollView.bounds.size.height) print(mainScrollView.contentSize) mainScrollView.backgroundColor = UIColor.blackColor() mainScrollView.delegate = self self.view.addSubview(mainScrollView) configScrollView() addPageControlOnScrollView() } 

你忘了在代码中添加这一行

  banerView.frame = cgrectmake(0,430,320,50); [self.view addsubView:bannerView]; 

通过在addPageControlOnScrollView下添加Admob解决了问题。

 func addPageControlOnScrollView() { self.pageControl.numberOfPages = photos.count self.pageControl.currentPage = 0 self.pageControl.tintColor = UIColor.redColor() self.pageControl.pageIndicatorTintColor = UIColor.whiteColor() self.pageControl.currentPageIndicatorTintColor = UIColor.greenColor() pageControl.addTarget(self, action: Selector("changePage:"), forControlEvents: UIControlEvents.ValueChanged) self.pageControl.frame = CGRectMake(0, self.view.frame.maxY - 84, self.view.frame.width, 44) //self.pageControl.backgroundColor = UIColor.yellowColor() self.view.addSubview(pageControl) bannerView.adUnitID = "ca-app-pub-MYID" bannerView.rootViewController = self bannerView.loadRequest(GADRequest()) view.addSubview(bannerView) }