3

I have a page view controller that transitions between view controllers using the normal UIPageViewController methods. Inside one of the presented view controllers (let's say the current view controller), I add a child view controller into a subview called containerView like so:

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let child = storyboard.instantiateViewController(withIdentifier: "child")
    self.addChildViewController(child)
    child.view.frame = self.containerView.bounds
    self.containerView.addSubview(child.view)
    child.didMove(toParentViewController: self)

The view controller is presented inside the containerView, however it is only semi-interactive. For example, let's say the child view controller has a slider that updates a label with the slider's value. When sliding the slider, the slider will move visually inside the containerView, but won't update the label (the IBAction connected to the slider doesn't run). This behavior worked correctly when the view controller was presented normally (full screen using normal segueing).

Any ideas?

Jeremy Kelleher
  • 287
  • 3
  • 13
  • it's very strange... so in debug, the delegate method of your slider-changed don't invoked? – Alessio Campanelli Nov 24 '16 at 21:55
  • actually it does get triggered, however the label does not update. I placed a breakpoint in the slider IBAction and it does get called, but the label's text does not change `label.text = String(slider.value)` like it does when the view controller is presented normally. I even tried to change the background color of `self.view` and that works correctly, just to see if that worked. – Jeremy Kelleher Nov 24 '16 at 22:33
  • You need to describe the situation in _much_ more precise detail in order to get a good answer. Otherwise you're just wasting the rep you've just spent on the bounty. It's hard to tell what the problem even is. However, note that communication between multiple view controller levels (view in a view controller in a parent view controller) is a very tricky problem! I have a long discussion of options here that might give you some ideas: http://stackoverflow.com/a/33458977/341994 – matt Nov 27 '16 at 03:08

2 Answers2

0

Seems as though something was going on with the storyboard view controllers, as I was able to remove the slider and add a new slider in and the functionality worked correctly inside the UIPageViewController page.

Jeremy Kelleher
  • 287
  • 3
  • 13
0

Maybe it is because you don't set constraints to child subview. And when container view size changed - your child view is out of bounds. You can check it is with View Debug feature in Xcode (when your app is in problem screen, you can capture all views (menu Debug->View debugging->Capture view hierarchy) and investigate what happens

child.view.frame = self.containerView.bounds
self.containerView.addSubview(child.view)
// need to add constraint, because if container view resized, child view frame not changed
child.view.autoresizinkMask = [.flexibleHeight, .flexibleWidth]
child.view.translatesAutoresizingMaskIntoConstraints = true
child.didMove(toParentViewController: self)
Vitalii Gozhenko
  • 9,220
  • 2
  • 48
  • 66