PageViewController透明或无背景

时间:2018-03-30 22:03:04

标签: swift swift3 uipageviewcontroller

我的PageViewController点指示器有问题。实际上有背景。我试过UIColor.clear,但这没有帮助。背景仍然是白色。我可以改变颜色,但我不知道如何删除它或使其透明。

找不到如何使其透明的方法。

附上截图。

如何使背景颜色透明或将其删除?

Screenshot

ViewController.swift

import Foundation
import UIKit

class ViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

   // Hide status bar.
override var prefersStatusBarHidden : Bool {
    return true
  }

var pageViewController: UIPageViewController!
let pages = ["DomesticPageViewController", "SafariPageViewController", "ForestPageViewController"]

// Page View Controller Datasource

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

    if let index = pages.index(of: viewController.restorationIdentifier!) {
        if index > 0 {
            return viewControllerAtIndex(index - 1)
        }
    }

    return nil
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

    if let index = pages.index(of: viewController.restorationIdentifier!) {
        if index < pages.count - 1 {
            return viewControllerAtIndex(index + 1)
        }
    }

    return nil
}


func viewControllerAtIndex(_ index: Int) -> UIViewController? {
    let vc = storyboard?.instantiateViewController(withIdentifier: pages[index])
    return vc

}



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    if let vc = storyboard?.instantiateViewController(withIdentifier: "MyPageViewController") {
        self.addChildViewController(vc)
        self.view.addSubview(vc.view)

        pageViewController = vc as! UIPageViewController
        pageViewController.dataSource = self
        pageViewController.delegate = self

        pageViewController.setViewControllers([viewControllerAtIndex(0)!], direction: .forward, animated: true, completion: nil)
        pageViewController.didMove(toParentViewController: self)


    }

}

    private func setupPageControl() {
        let appearance = UIPageControl.appearance()
        appearance.pageIndicatorTintColor = UIColor.lightGray
        appearance.currentPageIndicatorTintColor = UIColor.black
        appearance.backgroundColor = UIColor.clear


}

    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        setupPageControl()
        return self.pages.count
    }

    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        return 0
    }


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

DomesticViewController.swift

import Foundation
import UIKit

class DomesticPageViewController: UIViewController {


override func viewDidLoad() {
    super.viewDidLoad()

    let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
    backgroundImage.image = UIImage(named: "domestic_bg1")
    backgroundImage.contentMode = UIViewContentMode.scaleAspectFill
    self.view.insertSubview(backgroundImage, at: 0)

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

}

1 个答案:

答案 0 :(得分:-1)

如果您正在使用页面控件:

使用StoryBoard:

您可以使用StoryBoard简单地设置色调颜色,当前页面颜色和背景颜色。请参阅下面的图像

enter image description here

或其他以编程方式:

    pageControl.backgroundColor = UIColor.clear
    pageControl.currentPageIndicatorTintColor = UIColor.black
    pageControl.pageIndicatorTintColor = UIColor.white