Saketram Durbha
Saketram Durbha

Reputation: 450

Show View Controller in Tab Bar Controller From Another Tab Without Switching Tabs

I have four view controllers embedded in a Tab Bar Controller (as shown below). I want to show the profile view from the feed view, without switching the tab. Any suggestions would be great.

enter image description here

Upvotes: 1

Views: 1329

Answers (1)

O-mkar
O-mkar

Reputation: 5658

Best way to do is adding a subview

Here is my code i have used in my project tested on Swift 2.0

 let tabBarController = self.sourceViewController as TabBarController
 let destinationController = self.destinationViewController as UIViewController

        for view in tabBarController.placeholderView.subviews as [UIView] {
            view.removeFromSuperview() // 1st remove from superview
        }

        // Add view to placeholder view
        tabBarController.currentViewController = destinationController
        tabBarController.placeholderView.addSubview(destinationController.view) // 2

        // Set autoresizing mask so it fits correctly
        tabBarController.placeholderView.setTranslatesAutoresizingMaskIntoConstraints(false)
        destinationController.view.setTranslatesAutoresizingMaskIntoConstraints(false)

        let horizontalConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v1]-0-|", options: .AlignAllTop, metrics: nil, views: ["v1": destinationController.view]) // 3

        tabBarController.placeholderView.addConstraints(horizontalConstraint)

        let verticalConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v1]-0-|", options: .AlignAllTop, metrics: nil, views: ["v1": destinationController.view]) // 3

        tabBarController.placeholderView.addConstraints(verticalConstraint)

        tabBarController.placeholderView.layoutIfNeeded() // 3
        destinationController.didMoveToParentViewController(tabBarController) // 4

    }

Here is the Blog i have refer to when i was doing custom tabbar hope it will help you : http://swiftiostutorials.com/tutorial-custom-tabbar-storyboard/

Upvotes: 1

Related Questions