Search code examples
iosuitabbarcontrolleruistoryboarduistoryboardsegue

View controller view getting misplaced when navigating back to the home screen of UITabbarController


I have a tabbarcontroller in my application and have added view controllers with the help of storyboard and segues,

When i am in the second tabbar item I have a table view embedded with a navigation controller filled with usernames and on top I have a hamburger icon, I select a particular item from the table list present in the hamburger icon and navigate to the details view controller I have used segues and given below is the code for the same

self.performSegueWithIdentifier("MY_DETAIL_SEGUE_NAME", sender: nil)

MY_DETAIL_SEGUE_NAME is actually a custom push segue of the SWRevealViewControllerSeguePushController, the push is working fine and i am able to navigate to the details screen.

The Problem: Since I am in the second tabbar item on the hit of back button i need to show the inital view / home view controller of the second tabbar item which is the friend view list, so on the back button i have written the below code in the detail screen

  @IBAction func moveBackToController(sender: AnyObject) {
        self.performSegueWithIdentifier("showFriendListPage", sender: nil)
    }

showFriendListPage: This is a custom segue to the firendlist page using the SWRevealViewControllerSeguePushController and is pointed to the tabbarcontroller

To highlight the second tabbar item i have used the below code written in the detail screen

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    let objParentTabbar = segue.destinationViewController as! ParentTabbarController
    if segue.identifier == "showFriendListPage" {
    objParentTabbar.selectedIndex = 1
    }
}

What happening here is when i navigate back to the home screen of the second tabbar item my view is distorted i.e. the UI elements are not placed in a correct order which they were when i first went to the second tabbar

Note: All my view controllers are embedded inside the navigation controller so the structure is

TabbarController----->NavigationController------>ViewController

Please suggest where I am going wrong and what needs to be done to resolve this, my only struggle is the view getting distorted and i dont really know why is this happening.


Solution

  • Call the view your controller via code in your application should do the trick

    objParentTabbar.selectedViewController?.viewWillAppear(true)