ios - 快速滑动导航调整 View Controller 大小

标签 ios swift swipe viewcontroller

我正在尝试使用本教程使用 View Controller 而不是 xib 文件进行滑动导航:https://www.youtube.com/watch?v=3jAlg5BnYUU

我已经设法用 View Controller 替换 xib 文件,但是当我运行应用程序时, View Controller 的宽度小于正常宽度:

Screenshot

我不明白,因为它的设置与 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.
}


}

最佳答案

请注意,手动使用 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)
}

关于ios - 快速滑动导航调整 View Controller 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38640519/

相关文章:

ios - Segue 动画/故障和延长滑动手势长度上的黑屏

objective-c - 如何关闭自动滑动返回导航 Controller ? swift

Android解锁像滑动手势

javascript - 为什么我得到 'Cannot read property ' push' of undefined' (React Native)?

ios - 为什么文档说使用 vDSP_DFT 而不是 FFT

ios - App 与 iWatch 通信 : is using app group the only way?

ios - AlertController - 与 UIViewController 冲突

ios - UIStatusBar 作为 Facebook 新的 iOS7 应用程序

ios - 谷歌地图api在swift3中解析旅行持续时间数据

ios - 按钮被某些东西重叠并且无法正常工作