当在页面视图控制器中遍历时,背景图像隐藏iOS应用程序(用swift4编写)中的所有其他UI元素

时间:2018-05-30 12:17:19

标签: ios uipageviewcontroller

当遍历页面视图控制器的页面时,背景图像会隐藏所有其他UI元素。 可能的原因是什么? 仅当向后滑动页面并且设置背景图像并且内容模式设置为到scaleAspectFill 时才会发生这种情况,向前滑动页面工作正常。 如果我改变(backgroundImageView.contentMode = .scaleAspectFit)它可以正常工作。 我在View Did Load中设置了View Controller的背景图像。

以下是为pageviewcontroller实例化的View Controller的代码。

    class ViewController: UIViewController {

    @IBOutlet weak var dayDescription: UILabel!

    var descriptionPassed: descriptionPassed? = nil

    override func viewDidLoad() {
            super.viewDidLoad()
    setBackGround()
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)

            if let descInDetail = descriptionPassed {

                self.dayDescription.text = descInDetail.dataDetail[0].w_description
    }

     func setBackGround(){
        DispatchQueue.main.async {
                        print("In dispatch main)

                        let backgroundImage = UIImage.init(named: imageToPutInBackground)
                        let backgroundImageView = UIImageView.init(frame: self.view.frame)

                        backgroundImageView.image = backgroundImage
                        backgroundImageView.contentMode = .scaleAspectFill
                        backgroundImageView.alpha = 0.9

                        self.view.insertSubview(backgroundImageView, at: 0)
                    }
    }
}

页面视图控制器代码

class PageViewController: UIPageViewController,UIPageViewControllerDataSource, UIPageViewControllerDelegate{

    var desc = [Desc]()
    var passSignalToHideButtonInContentVC : Bool = false
    var pageControl = UIPageControl()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.dataSource = self
        self.delegate = self

        //self.configurePageControl()

        let initialContenViewController = self.pageAtIndex(0) as ViewController
        self.setViewControllers([initialContenViewController], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
        self.pageControl.pageIndicatorTintColor = UIColor.red
        self.pageControl.currentPageIndicatorTintColor = UIColor.black  

    }

    func pageAtIndex(_ index: Int) ->ViewController
    {

        let pageContentViewController = self.storyboard?.instantiateViewController(withIdentifier: "PageContent") as! ViewController

        if desc.count == 0 {

            pageContentViewController.pageIndex = 0

            return pageContentViewController

        }else {

            let desc = self.desc[index]
            pageContentViewController.desc = desc
            pageContentViewController.pageIndex = index
            pageContentViewController.hideFavButton = passSignalToHideButtonInContentVC

            return pageContentViewController

        }
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
    {
        let viewController = viewController as! ViewController
        var index = viewController.pageIndex as Int
        pageControl.currentPage = index 
        if(index == 0 || index == NSNotFound)
        {
            return nil
        }

        index -= 1

        return self.pageAtIndex(index)
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
    {
        let viewController = viewController as! ViewController
        var index = viewController.pageIndex as Int
        pageControl.currentPage = index //move dot
        if((index == NSNotFound))
        {
            return nil
        }

        index += 1

        if(index == desc.count)
        {
            return nil
        }

        return self.pageAtIndex(index)
    }

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

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

}

1 个答案:

答案 0 :(得分:1)

我终于通过将clipsToBound属性设置为true来解决这个问题。

backgroundImageView.clipsToBounds = true