まずUIViewControllerで拡張変数作成
addedBool
public extension UIViewController { private struct additional { static var addedBool: Bool = false } var addedBool: Bool { get { guard let theBool = objc_getAssociatedObject(self, &additional.addedBool) as? Bool else { return false } return theBool } set { objc_setAssociatedObject(self, &additional.addedBool, newValue, .OBJC_ASSOCIATION_RETAIN) } } }
isMovingFromParent
でやっているのを以下のようにする。
override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) if self.isMovingFromParent { // 戻るときにタブバーを戻す if let _ = self.navigationController?.tabBarController { let nav = self.navigationController // 一つ前のViewControllerを取得する if let count = nav?.viewControllers.count { let createGroupViewController = nav?.viewControllers[count - 1] createGroupViewController?.addedBool = true } } } }
戻ってきたページ系のViewControllerの viewDidAppear
の中で以下
// 戻ってからTabBar隠す if self.addedBool { if let tabBarVc = self.navigationController?.tabBarController { tabBarVc.tabBar.isHidden = false } self.addedBool = false // backup let viewBounds = self.view.bounds // タブバー位置情報 if let tabBarFrame = (tabBarController?.tabBar.frame), // ナビゲーションバー位置情報 let navBarFrame = (navigationController?.navigationBar.frame) { self.view.frame = CGRect(x: 0, y: navBarFrame.origin.y + navBarFrame.height, width: viewBounds.width, height: viewBounds.height - tabBarFrame.height) } }