如何在UIPageViewController上隐藏状态栏? (所有视图控制器)

时间:2017-11-29 10:26:36

标签: ios swift statusbar uipageviewcontroller

我已经搜索了我在这里可以找到的每个问题/答案,但我无法弄清楚如何隐藏UIPageViewController中所有视图控制器的状态栏。这是我的UIPageViewController类的代码:

    class TipsVC: UIPageViewController, UIPageViewControllerDelegate {

        lazy var VCArr: [UIViewController] = {
            return [self.VCInstance(name: "T1"),
                    self.VCInstance(name: "T2"),
                    self.VCInstance(name: "T3")]
        }()

        private func VCInstance(name: String) -> UIViewController {
            return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)

 }
        override public func viewDidLoad() {
            super.viewDidLoad()
            //self.dataSource = self
            self.delegate = self
            if let OB1 = VCArr.first {
                setViewControllers([OB1], direction: .forward, animated: true, completion: nil)
                let pageController = UIPageControl.appearance()
                pageController.pageIndicatorTintColor = UIColor(red:1.00, green:0.88, blue:0.92, alpha:1.0)
                pageController.currentPageIndicatorTintColor = UIColor(red:1.00, green:0.25, blue:0.51, alpha:1.0)
            }


        }

        override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
            for view in self.view.subviews {
                if view is UIScrollView {
                    view.frame = UIScreen.main.bounds
                } else if view is UIPageControl {
                    view.backgroundColor = UIColor.clear
                }
            }
        }

        public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{
            guard let viewControllerIndex = VCArr.index(of: viewController) else {
                return nil
            }

            let previousIndex = viewControllerIndex - 1

            guard previousIndex >= 0 else {
                return VCArr.last
            }

            guard VCArr.count > previousIndex else {
                return nil
            }

            return VCArr[previousIndex]
        }

        public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{
            guard let viewControllerIndex = VCArr.index(of: viewController) else {
                return nil
            }

            let nextIndex = viewControllerIndex + 1

            guard nextIndex < VCArr.count else {
                return VCArr.first
            }

            guard VCArr.count > nextIndex else {
                return nil
            }

            return VCArr[nextIndex]
        }

        public func presentationCount(for pageViewController: UIPageViewController) -> Int{

            return VCArr.count

        }

        public func presentationIndex(for pageViewController: UIPageViewController) -> Int{

            guard let OB1 = viewControllers?.first,
                let OB1Index = VCArr.index(of: OB1) else {
                    return 0

            }

            return OB1Index
        }

        public func nextPageWithIndex(index: Int)
        {
            let nextVC = VCArr[index]
            setViewControllers([nextVC], direction: .forward, animated: true, completion: nil)

        }


    }

以及来自我的一个viewControllers的代码示例:

    class T1: UIViewController {

        @IBOutlet var nextBtn: UIButton!


        override func viewDidLoad() {
            super.viewDidLoad()

        }

        override func viewDidAppear(_ animated: Bool) {

            super.viewDidAppear(true)
            UIApplication.shared.isStatusBarHidden = true

        }

        @IBAction func nextBtnDidPress(_ sender: Any) {


            let next = self.parent as! TipsVC
            next.nextPageWithIndex(index: 1)

    }

}

我很清楚如何隐藏常规视图控制器上的状态栏,但是当我使用UIPageViewController时,我无法获得相同的结果。发生了什么事?

1 个答案:

答案 0 :(得分:0)

要从particluar viewController隐藏状态栏,您需要隐藏viewWillAppear中的状态栏并隐藏viewWillDisAppear中的状态栏

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    UIApplication.shared.isStatusBarHidden = true
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.shared.isStatusBarHidden = false
}

viewWillAppear将在打开特定的viewController之前调用,并在解除特定的viewController后调用viewWillDisAppear

如果您遇到UIPageViewController问题,请使用UIViewController并在其中嵌入UIPageViewController以供参考检查以下代码

        var pageContainer: UIPageViewController!

        pageContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
        pageContainer.delegate = self
        pageContainer.dataSource = self
        pageContainer.setViewControllers([getViewControllerAtIndex(index: 0)] as [UIViewController], direction: .forward, animated: false, completion: nil)
        pageContainer.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
        self.view.addSubview(pageContainer.view)

希望这会对你有所帮助