Add swift child view controller

I am trying to add a child view controller to a containerViewController

Child:

AViewController
BViewController
CViewController

      

I have no error, but when I run the application I can swipe across the screen, there are 3 sections, but view controllers A, B, C do not appear.

This is my code, any idea?

import UIKit

class ContainerViewController: UIViewController {

@IBOutlet var scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    // creat VC

    var Avc : AViewController = AViewController ()
    var Bvc : BViewController = BViewController ()
    var Cvc : CViewController = CViewController ()



    // add it to the view hierarchie


    self.addChildViewController(Cvc)
    self.scrollView.addSubview(Cvc.view)
    Cvc.didMoveToParentViewController(self)

    self.addChildViewController(Bvc)
    self.scrollView.addSubview(Bvc.view)
    Bvc.didMoveToParentViewController(self)


    self.addChildViewController(Avc)
    self.scrollView.addSubview(Avc.view)
    Avc.didMoveToParentViewController(self)

    // set the frame

    var adminFrame : CGRect = Avc.view.frame
    adminFrame.origin.x = adminFrame.width
    Bvc.view.frame = adminFrame

    var BFrame : CGRect = Bvc.view.frame
    BFrame.origin.x = 2*BFrame.width
    Cvc.view.frame = BFrame

    // set the frame of the scrollview


    var scrollWidth: CGFloat = 3*self.view.frame.width
    var scrollHeight: CGFloat = self.view.frame.size.height
    self.scrollView.contentSize = CGSizeMake(scrollWidth, scrollHeight)


}

      

Edit:

Looking at the view hierarchy, it reports the following:

<UIWindow: 0x7ff3fad19f70; frame = (0 0; 320 568); gestureRecognizers = <NSArray: 0x7ff3fac3efe0>; layer = <UIWindowLayer: 0x7ff3fad19740>>
   | <UIView: 0x7ff3fb108b90; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x7ff3fb108e60>>
   |    | <UIScrollView: 0x7ff3fac3acf0; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7ff3fb107320>; layer = <CALayer: 0x7ff3fac18e00>; contentOffset: {0, 0}; contentSize: {960, 568}>
   |    |    | <UIView: 0x7ff3fac41ed0; frame = (640 0; 320 568); autoresize = W+H; layer = <CALayer: 0x7ff3fac28a00>>
   |    |    | <UIView: 0x7ff3fac42320; frame = (320 0; 320 568); autoresize = W+H; layer = <CALayer: 0x7ff3fac38e10>>
   |    |    | <UIView: 0x7ff3fac42730; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x7ff3fac42810>>
   |    |    | <UIImageView: 0x7ff3faf020f0; frame = (0 564.5; 320 3.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x7ff3fae0df80>>
   |    |    | <UIImageView: 0x7ff3fac1c660; frame = (316.5 0; 3.5 568); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x7ff3fac39420>>
   |    | <_UILayoutGuide: 0x7ff3fb108ec0; frame = (0 0; 0 20); hidden = YES; layer = <CALayer: 0x7ff3fb1091e0>>
   |    | <_UILayoutGuide: 0x7ff3fb109c20; frame = (0 568; 0 0); hidden = YES; layer = <CALayer: 0x7ff3fb109d00>>

      

+1


source to share


2 answers


When I use your code, start the application and pause the application and look at the view hierarchy, I see this:

(lldb) po [[UIWindow keyWindow] recursiveDescription]
<UIWindow: 0x156bdc30; frame = (0 0; 320 568); gestureRecognizers = <NSArray: 0x156be750>; layer = <UIWindowLayer: 0x156aa3c0>>
   | <UIView: 0x156c5440; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x156c55c0>>
   |    | <UIScrollView: 0x156c2740; frame = (0 0; 0 568); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x156c4d00>; layer = <CALayer: 0x156c2a80>; contentOffset: {0, 0}; contentSize: {960, 568}>
   |    |    | <UIView: 0x156c6df0; frame = (640 0; 0 536); autoresize = W+H; layer = <CALayer: 0x156c6d80>>
   |    |    | <UIView: 0x156c7100; frame = (320 0; 0 536); autoresize = W+H; layer = <CALayer: 0x156c70a0>>
   |    |    | <UIView: 0x156c73f0; frame = (0 0; 0 536); autoresize = W+H; layer = <CALayer: 0x156c7390>>
   |    |    | <UIImageView: 0x156c8bd0; frame = (0 564.5; 600 3.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x156c8c50>>
   |    |    | <UIImageView: 0x156c9020; frame = (-3.5 32; 3.5 568); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x156c90a0>>
   |    | <_UILayoutGuide: 0x156c5620; frame = (0 0; 0 20); hidden = YES; layer = <CALayer: 0x156c5800>>
   |    | <_UILayoutGuide: 0x156c5c90; frame = (0 568; 0 0); hidden = YES; layer = <CALayer: 0x156c5d10>>

      

If you don't see your subheadings there, then the likely culprits could be:

  • You may not have specified the base view controller class in your scene, and thus this code does not run. You can confirm this with a log statement println

    or breakpoint aside of this viewDidLoad

    and make sure you hit that routine.

  • You may not have connected @IBOutlet

    for scrollView

    , and thus scrollView

    - nil

    . Confirm this by putting a breakpoint at viewDidLoad

    and looking at the property scrollView

    .




In your revised question, we can now see that three sub-points are present and appear to be correct. It's great.

Now the question is why you can't see anything. If they are defined as scenes in your storyboard, you should:

  • Make sure to have a base class and storyboard ID defined for each of these three child scenes; and

  • When your main view controller creates three child view controllers, you must create them from the storyboard using the storyboard ID (in my example, I used storyboard IDs A

    , B

    and C

    accordingly):

    let Avc = storyboard.instantiateViewControllerWithIdentifier("A") as AViewController
    let Bvc = storyboard.instantiateViewControllerWithIdentifier("B") as BViewController
    let Cvc = storyboard.instantiateViewControllerWithIdentifier("C") as CViewController
    
          

If you do the above and repeat recursiveDescription

, you will see the scene sub-items (like added shortcuts) appear in the output.

+3


source


Use the following extension to add ChildviewController create Extensions.swift file copy following code

import UIKit

extension UIViewController {
    func configureChildViewController(childController: UIViewController, onView: UIView?) {
        var holderView = self.view
        if let onView = onView {
            holderView = onView
        }
        addChildViewController(childController)
        holderView?.addSubview(childController.view)
        constrainViewEqual(holderView: holderView!, view: childController.view)
        childController.didMove(toParentViewController: self)
    }


    func constrainViewEqual(holderView: UIView, view: UIView) {
        view.translatesAutoresizingMaskIntoConstraints = false
        //pin 100 points from the top of the super
        let pinTop = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal,
                                    toItem: holderView, attribute: .top, multiplier: 1.0, constant: 0)
        let pinBottom = NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal,
                                       toItem: holderView, attribute: .bottom, multiplier: 1.0, constant: 0)
        let pinLeft = NSLayoutConstraint(item: view, attribute: .left, relatedBy: .equal,
                                     toItem: holderView, attribute: .left, multiplier: 1.0, constant: 0)
        let pinRight = NSLayoutConstraint(item: view, attribute: .right, relatedBy: .equal,
                                      toItem: holderView, attribute: .right, multiplier: 1.0, constant: 0)

        holderView.addConstraints([pinTop, pinBottom, pinLeft, pinRight])
    }
}

      



Custom views controller

    import UIKit

    class MyViewControler:UIViewControler  {

    @IBOutlet weak var myView: UIView!



    override func viewDidLoad() {
        super.viewDidLoad()

        let demoViewInstance = storyboard!.instantiateViewController(withIdentifier: "youChiledViewController_Id") as! childViewController
        configureChildViewController(childController: demoViewInstance, onView: myView)

    }

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

}

      

+4


source







All Articles