如何使用scrollView使嵌入导航控制器工作

时间:2017-12-14 13:53:49

标签: ios swift uiscrollview uinavigationcontroller overlap

我看过这个教程 Snapchat Like Layout using View Controller Theme 然后我在导航控制器中嵌入了三个控制器中的一个,但它没有工作然后我试图在视图控制器上添加它如何滚动视图也不起作用+给我重叠我没有在代码中尝试它但是我不认为这是原因,这是代码

i hope this may help

class ViewController: UIViewController {


    @IBOutlet weak var scrollView: UIScrollView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let V1 = self.storyboard?.instantiateViewController(withIdentifier: "V1") as UIViewController!
        self.addChildViewController(V1!)
        self.scrollView.addSubview((V1?.view)!)
        V1?.didMove(toParentViewController: self)
        V1?.view.frame = scrollView.bounds

        let V2 = self.storyboard?.instantiateViewController(withIdentifier: "V2") as UIViewController!
        self.addChildViewController(V2!)
        self.scrollView.addSubview((V2?.view)!)
        V2?.didMove(toParentViewController: self)
        V2?.view.frame = scrollView.bounds

        var V2Frame: CGRect = V2!.view.frame
        V2Frame.origin.x =  self.view.frame.width
        V2?.view.frame = V2Frame

        let V3 = self.storyboard?.instantiateViewController(withIdentifier: "V3") as UIViewController!
        self.addChildViewController(V3!)
        self.scrollView.addSubview((V3?.view)!)
        V3?.didMove(toParentViewController: self)
        V3?.view.frame = scrollView.bounds

        var V3Frame: CGRect = V3!.view.frame
        V3Frame.origin.x = 2 * self.view.frame.width
        V3?.view.frame = V3Frame

        self.scrollView.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.height)
        self.scrollView.contentOffset = CGPoint(x: self.view.frame.width * 1 , y: self.view.frame.height)

    }

代码如何工作? 1-首先我将滚动视图添加到视图控制器 2-然后在滚动视图中我添加了3个视图控制器,所有3个控制器都有自己的类,如普通视图控制器
3-滚动视图顶部的代码在其视图中添加了3个控制器 所有3个控制器继承自己的类,一切都很好 如果有人可以帮助或需要更多描述,请询问

1 个答案:

答案 0 :(得分:0)

try below


Create ViewController in Storyboard or XIB and add ScrollView and PageControl

My Class as below:


class ScrollContentViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var pageControl: UIPageControl!
    @IBOutlet weak var scrollView: UIScrollView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }


    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.pageControl.currentPage = 0
        self.pageControl.numberOfPages = 4
        var originX = CGFloat(0)
        let width = scrollView.frame.size.width
        let height = scrollView.bounds.size.height
        for x in 0 ..< 4 {
            let V1 = UIViewController()
            self.scrollView.addSubview(V1.view)
            V1.view.frame = CGRect(x: originX, y: 0, width: width, height: height)
            V1.view.backgroundColor = x % 2 == 0 ? UIColor.lightGray : UIColor.darkGray
            self.addChildViewController(V1)
            originX += width
        }
        self.scrollView.contentSize = CGSize(width: originX, height: height)
    }

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

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let x = Int(scrollView.contentOffset.x / scrollView.frame.size.width)
        self.pageControl.currentPage = x
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}