Swift滑动导航调整视图控制器大小

时间:2016-07-28 15:19:38

标签: ios swift swipe viewcontroller

我尝试使用本教程使用视图控制器而不是xib文件进行滑动导航:here

我设法用视图控制器替换xib文件,但是当我运行应用程序时,视图控制器的宽度小于正常值:

https://www.youtube.com/watch?v=3jAlg5BnYUU

我不明白,因为它与xib文件的设置完全相同。谁知道如何解决这个问题?

这是我的代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()

    let vc0 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController0")

    self.addChildViewController(vc0!)
    self.scrollView.addSubview(vc0!.view)
    vc0!.didMoveToParentViewController(self)


    let vc1 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController1")

    var frame1 = vc1!.view.frame
    frame1.origin.x = self.view.frame.size.width
    vc1!.view.frame = frame1

    self.addChildViewController(vc1!)
    self.scrollView.addSubview(vc1!.view)
    vc1!.didMoveToParentViewController(self)


    let vc2 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController2")

    var frame2 = vc2!.view.frame
    frame2.origin.x = self.view.frame.size.width * 2
    vc2!.view.frame = frame2

    self.addChildViewController(vc2!)
    self.scrollView.addSubview(vc2!.view)
    vc2!.didMoveToParentViewController(self)


    let vc3 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController3")

    var frame3 = vc3!.view.frame
    frame3.origin.x = self.view.frame.size.width * 3
    vc3!.view.frame = frame3

    self.addChildViewController(vc3!)
    self.scrollView.addSubview(vc3!.view)
    vc3!.didMoveToParentViewController(self)


    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height - 66)


}

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


}

1 个答案:

答案 0 :(得分:0)

请注意,手动使用viewDidLoad设置帧有点风险,应该避免,因为没有设置边界。而是使用viewDidLayoutSubviews来执行此操作,但请查看,因为在UI元素更改(包括约束)时会一直调用该方法。

    var scrollViewAdded = false

override func viewDidLayoutSubviews() {

    super.viewDidLayoutSubviews()

    if !scrollViewAdded {

        self.loadSrollView()

        self.scrollViewAdded = true
    }
}


func loadSrollView() {

    self.scrollView.pagingEnabled = true

    let vc0 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController0")

    self.addChildViewController(vc0!)
    self.scrollView.addSubview(vc0!.view)
    vc0!.didMoveToParentViewController(self)

    let vc1 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController1")

    var frame1 = vc1!.view.frame
    frame1.origin.x = self.view.frame.size.width
    vc1!.view.frame = frame1

    self.addChildViewController(vc1!)
    self.scrollView.addSubview(vc1!.view)
    vc1!.didMoveToParentViewController(self)

    let vc2 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController2")

    var frame2 = vc2!.view.frame
    frame2.origin.x = self.view.frame.size.width * 2
    vc2!.view.frame = frame2

    self.addChildViewController(vc2!)
    self.scrollView.addSubview(vc2!.view)
    vc2!.didMoveToParentViewController(self)

    let vc3 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController3")

    var frame3 = vc3!.view.frame
    frame3.origin.x = self.view.frame.size.width * 3
    vc3!.view.frame = frame3

    self.addChildViewController(vc3!)
    self.scrollView.addSubview(vc3!.view)
    vc3!.didMoveToParentViewController(self)

    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height - 66)
}