简体   繁体   中英

UITabBarController height decreases everytime it's loaded

When I first load my application and log in. Everything is fine.

However when I log out, then log back in. The height of my view has been decreased. Here's a screenshot of the bug:

在此处输入图片说明

I havn't been able to find the cause of this. Making this quite a difficult question to ask help with as I can't specify the precise section of code causing the issue. But I'll try.

The problematic setup is like so:

I have a containerViewController , with 2 childViewControllers, a menu and a UITabBarController . The UITabBarController has 2 UIViewControllers .

To better explain it, here's a visual representation.

_______________________

App Start -> 

NavigationController(rootViewController LandingPageVC)

   LandingPageVC -> push -> SignInVC(this is where I login from)

   SignInVC -> push -> ContainerViewController(this has my UITabBarController and my menu)

   ContainerViewController (sets up my menuTabBarController and menu)

        menuTabBarController (this tabBarController is used to switch out my content from the menu)

        SidePanelViewController (this is my menu)

   ContainerViewController -> push(signing out) -> LandingPageVC

_______________________

Here's how I push the containerViewController when a successful login is called.

let mainTableViewController = ContainerViewController()
mainTableViewController.navigationItem.setHidesBackButton(true, animated: false)
navigationController!.pushViewController(mainTableViewController, animated: true)
menuEnabled = true

here's the function called from the containerViewController I use to log out.

func signOut() {

    // Set up the landing page as the main viewcontroller again.
    let mainTableViewController = LandingPageVC()
    mainTableViewController.navigationItem.setHidesBackButton(true, animated: false)
    mainTableViewController.skipView = false
    self.navigationController!.pushViewController(mainTableViewController, animated: true)

    // Disable menu access
    menuEnabled = false
}

by printing the height of ContainerViewController and menuTabBarController, I found that it is the UITabBarController's height that's decreasing and not the ContainerViewController.

Here's the code that has to do with the UITabBarController

import UIKit
import QuartzCore

let menuTabBarController = UITabBarController()

var menuButton = UIBarButtonItem()

var menuEnabled = false

class ContainerViewController: UIViewController, CenterViewControllerDelegate, SidePanelViewControllerDelegate, UIGestureRecognizerDelegate {

    func needsSignOut(sender: SidePanelViewController) {
        // toggling left panel
        self.toggleLeftPanel()
        // signing out
        self.signOut()
    }


  var centerViewController: UITabBarController!

  var leftViewController: SidePanelViewController?

  let centerPanelExpandedOffset: CGFloat = 60

  override func viewDidLoad() {
    super.viewDidLoad()

    menuTabBarController.tabBar.hidden = true

    menuButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: self, action: "toggleLeftPanel")
    if let font = UIFont(name: "FontAwesome", size: 20) {
        menuButton.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
    }
    self.navigationItem.leftBarButtonItem = menuButton

    //let tabBarController = UITabBarController()
    let suggestionsVC = SuggestionsViewController()
    let testVC = detaiLSuggestion_VC()
    let controllers = [suggestionsVC,testVC]

    menuTabBarController.setViewControllers(controllers, animated: false)

    centerViewController = menuTabBarController
    view.addSubview(menuTabBarController.view)
    addChildViewController(menuTabBarController)

    //centerNavigationController.didMoveToParentViewController(self)
  }

  // MARK: CenterViewController delegate methods

  func toggleLeftPanel() {

    let notAlreadyExpanded = (currentState != .LeftPanelExpanded)

    if notAlreadyExpanded {
        addLeftPanelViewController()
    }

    animateLeftPanel(shouldExpand: notAlreadyExpanded)

  }

  func collapseSidePanels() {
    switch (currentState) {
    case .LeftPanelExpanded:
      toggleLeftPanel()
    default:
      break
    }
  }

  func addLeftPanelViewController() {
    if (leftViewController == nil) {
        leftViewController = SidePanelViewController()
        leftViewController!.delegate = self

        addChildSidePanelController(leftViewController!)
    }
  }

  func addChildSidePanelController(sidePanelController: SidePanelViewController) {

    view.insertSubview(sidePanelController.view, atIndex: 0)

    addChildViewController(sidePanelController)
    sidePanelController.didMoveToParentViewController(self)
  }

  func animateLeftPanel(#shouldExpand: Bool) {
    if (shouldExpand) {
      currentState = .LeftPanelExpanded

        animateCenterPanelXPosition(targetPosition: CGRectGetWidth(centerViewController.view.frame) - centerPanelExpandedOffset)
    } else {
      animateCenterPanelXPosition(targetPosition: 0) { finished in
        self.currentState = .BothCollapsed

        self.leftViewController!.view.removeFromSuperview()
        self.leftViewController = nil;
      }
    }
  }


  func animateCenterPanelXPosition(#targetPosition: CGFloat, completion: ((Bool) -> Void)! = nil) {
    UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .CurveEaseInOut, animations: {
        self.centerViewController.view.frame.origin.x = targetPosition
      }, completion: completion)
  }

Any help deducing where this is coming from or how I can go about fixing it would be greatly appreciated! And again I apologize for the dumb of code. I'll update it further if I am able to rule out parts of it.

rdelmar found a solution for me in chat.

The problem was fixed by specifying the menuTabBarController.view.frame like so:

menuTabBarController.view.frame = self.view.frame

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM