2015-12-25 50 views
0

instruction from GoogleStoryboard方法。如何顯示Admob橫幅頂部沒有使用Storyboard?我試圖把它放在的this code未使用Storyboard加載Admob(Swift)

我試圖把這個放在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() 

} 
+0

您需要在視圖控制器中使用addsubView()添加bannerView。 – Harry

回答

0

問題通過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) 
} 
0

你忘了添加此行代碼

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

    [self.view addsubView:bannerView]; 
+0

如何使用Swift添加子視圖? – user2872856

+0

self.view.addSubview(bannerView) –

+0

請檢查問題編輯。我已添加子視圖,但仍然無效。其實我試圖在https://github.com/Charles-Hsu/ScrollViewDemo/blob/master/ScrollViewDemo/ScrollViewController.swift顯示Admob – user2872856