简体   繁体   中英

Loading Admob without using Storyboard (Swift)

The instruction from Google is the Storyboard method. How to show Admob banner on top without using Storyboard ? I am trying to put it in the ScrollView in this code .

I tried to put this under viewDidLoad , but it does not work.

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()

}

you forgot to add this line in code

   banerView.frame = cgrectmake(0,430,320,50);

    [self.view addsubView:bannerView];

Problem solved by adding Admob under addPageControlOnScrollView .

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM