1
votes

I have a view controller like below.

enter image description here

This view is attached with a tabBarController. The tabBarController has 5 viewControllers and I have to present the 5th viewController of tabBar from another page. So I used the below codes for present that viewController

@IBAction func onClickUserProfile(_ sender: Any) {
        let navVc = self.storyboard?.instantiateViewController(withIdentifier: "ProfileVC")as! ProfileVC
        navVc.userId = Int(self.userId)
        navVc.navigationItem.hidesBackButton = true
        navVc.tabBarController?.tabBar.isHidden = false
        self.navigationController?.pushViewController(nxtVc, animated: true)
    }

But after execute the code it resulting the view controller as the below image. The view undergoes the tabBar. Anyone help me to push to a tabBar view.

enter image description here

1
try to set automaticallyAdjustsScrollViewInsets = false, in ProfileVC(viewDidLoad)manishsharma93
Nothing happens.The view return the same resultAngel F Syrus
Why don't you use the present function to present your view controller?KingHodor
While using present it hides the tabBar and navigationBarAngel F Syrus
What is the bottom constraint of the "Sell Your Staff" button ?KingHodor

1 Answers

1
votes

You need to set the selected UIViewController from UITabBarController something like this should work .

self.tabBarController?.selectedViewController = self.tabBarController?.viewControllers![1]

where tabBarController?.viewControllers returns the array of current ViewControllers embedded in the UITabBarController .

Your code should be something like this.

@IBAction func onClickUserProfile(_ sender: Any) {
        let vc =  self.tabBarController?.viewControllers![1] as! ProfileVC // use your index
        vc.userId = Int(self.userId)
        self.tabBarController?.selectedViewController = vc
    }

Note: Don't create an instance of the UIViewController as .instantiateViewController(withIdentifier:) use the already existed ones in the array tabBarController?.viewControllers, creating new instance will be treated as new one and gives the problem you have up there .