为什么我的viewcontrollers没有显示在scrollview控制器上?

时间:2015-07-24 12:03:29

标签: ios swift uiviewcontroller uiscrollview uipagecontrol

我有三个步进视图控制器。我尝试使用滚动视图在单个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: 以下代码有效。它加载图像并按照预期显示它们。但是,在从其他视图控制器加载视图时,我不知道可能存在什么问题。

@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
}

2 个答案:

答案 0 :(得分:0)

尝试移动线:

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

在这一行之上:

for index in 0..<stepViewControllers.count {

此问题可能来自您在发送contentSize之前添加子视图的事实。

答案 1 :(得分:0)

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