为什么我的视图控制器没有显示在滚动视图控制器上?

Why my viewcontrollers are not being shown on scrollview controller?

我有三步视图控制器。我正在尝试使用滚动视图在单个 viewcontroller (RegisterViewController) 上显示这三个步骤,以便用户可以轻扫这些步骤。

我看到了一些其他的帖子,并走到了这一步。当我 运行 应用程序时,我得到一个带有 RegisterViewController 背景色的空白屏幕。我在步骤视图控制器中写了一些 println 语句来检查它们是否被实例化,我可以在控制台上看到这些语句的输出。请问有人能帮忙吗?

class RegisterViewController: UIViewController {

  @IBOutlet weak var scrollView: UIScrollView!
  @IBOutlet weak var registerStepsPageControl: UIPageControl!

  override func viewDidLoad() {
    super.viewDidLoad()
    setupScrollView()
  }

  func setupScrollView() {
    let storyboard = UIStoryboard(name: "Login", bundle: nil)
    let stepOneViewController = storyboard.instantiateViewControllerWithIdentifier("RegisterStepOne") as! RegisterStepOneViewController
    let stepTwoViewController = storyboard.instantiateViewControllerWithIdentifier("RegisterStepTwo") as! RegisterStepTwoViewController
    let stepThreeViewController = storyboard.instantiateViewControllerWithIdentifier("RegisterStepThree") as! RegisterStepThreeViewController

    var stepViewControllers = [stepOneViewController, stepTwoViewController, stepThreeViewController]

    for index in 0..<stepViewControllers.count {
      self.addChildViewController(stepViewControllers[index])

      let originX: CGFloat = CGFloat(index) * CGRectGetWidth(self.scrollView.frame);
      stepViewControllers[index].view.frame = CGRectMake(originX, 0, self.view.frame.size.width, self.view.frame.size.height);
      stepViewControllers[index].view.frame.size = self.view.frame.size
      self.scrollView.addSubview(stepViewControllers[index].view)
      stepViewControllers[index].didMoveToParentViewController(self)
    }
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * CGFloat(stepViewControllers.count), self.view.frame.height)
    self.scrollView.delegate = self
    self.registerStepsPageControl.numberOfPages = stepViewControllers.count
  }
}

EDIT01: 下面的代码有效。它加载图像并按预期显示它们。但是我不知道从其他 viewcontrollers.

加载视图时可能会出现什么问题
@IBOutlet weak var tutorialStepsPageControl: UIPageControl!

@IBOutlet weak var scrollView: UIScrollView!

var stepImages: [UIImage] = [UIImage(named: "tutorial_image_step1")!, UIImage(named: "tutorial_image_step2")!, UIImage(named: "tutorial_image_step3")!]


override func viewDidLoad() {
  super.viewDidLoad()
  self.tutorialStepsPageControl.numberOfPages = stepImages.count
  setupScrollView()
}

func setupScrollView() {
  var imageFrame: CGRect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
  var imageView: UIImageView!

  for index in 0..<stepImages.count {
    imageFrame.origin.x = self.view.frame.size.width * CGFloat(index)
    imageFrame.size = self.view.frame.size

    imageView = UIImageView(frame: imageFrame)
    imageView.image = stepImages[index]
    imageView.contentMode = .ScaleAspectFit
    self.scrollView.addSubview(imageView)
  }

  self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * CGFloat(stepImages.count), self.view.frame.size.height)
  self.scrollView.delegate = self
}

尝试移动线:

self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * CGFloat(stepViewControllers.count), self.view.frame.height)

这一行以上:

for index in 0..<stepViewControllers.count {

这个问题可能是因为您在发送 contentSize 之前添加了子视图。

好的,我删除了步进控制器并从头开始重新添加它们。做了一个干净的构建,现在它正在工作。不知道我做错了什么。 :/