user7219266
user7219266

Reputation:

iOS - How to avoid putting the Navigation Control in the Navigation Bar Items when working with Container View

I am currently working on a new feature which is presented like this :

enter image description here

The whole content is a Container View, in order to change the content by playing with the Segmented Control. I have 2 other UIViewController (for the first and second content).

Obviously embedded in a Navigation Controller.

My issue is that I can't put down the Segmented Control, I mean outside of the Navigation Bar.

If I bring it down, it appears hidden.

Here is some code of my main View Controller (the one containing the container view) :

 private func addViewControllerAsChildViewController(childViewController: UIViewController) {  
        view.addSubview(childViewController.view)

        childViewController.view.frame = view.bounds
        childViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]            
    }

    private func removeViewControllerAsChildViewController(childViewController: UIViewController) {
        childViewController.willMove(toParentViewController: nil)

        childViewController.view.removeFromSuperview()

        childViewController.removeFromParentViewController()
    }

What I am doing is after I instantiate my 2 ViewControllers (first and second), I add them as child View Controller.

How can I handle the Segmented Control without ruining all my logic and my design, I mean make it visible outside of the navigation bar.

Upvotes: 0

Views: 154

Answers (1)

Evan Anger
Evan Anger

Reputation: 714

I'd handle it as follows. In your container view add your segmented control, since you know this view is going to have a navigation bar use the simulated metrics so you know where the segmented control will be visible.

Create another view in this container view controller, which will contain your child view controllers. Make the view extent from the bottom of your segmented view to the bottom of the container view. IBOutlet this view (childContainerView) so you have access to it in your view controller code.

Instead of simply adding the child view controllers to your default self.view.addSubView(childVC.view) where self.view is the whole view, just add it to the aforementioned IBOutletted view (childContainerView).

Upvotes: 1

Related Questions