隐藏/显示状态栏使导航栏向下跳

时间:2020-10-03 20:02:23

标签: ios swift uinavigationcontroller uinavigationbar uistatusbar

我有一个UIViewControllerCameraViewController)和一个UINavigationControllerListsNavController)。它们被嵌入到另一个视图控制器MasterViewController中,如下所示:

class MasterViewController {
    /// I also have `removeFromParent` and other cleanup, but I don't think it's relevant to my question...
    func embedCamera() { 
        self.addChild(cameraViewController)
        view.insertSubview(cameraViewController.view, at: 0)
            
        cameraViewController.view.frame = view.bounds
        cameraViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            
        cameraViewController.view.layoutIfNeeded()
    }
    func embedLists() {
        self.addChild(listsNavController)
        view.addSubview(newViewController.view)
            
        listsNavController.view.frame = view.bounds
        listsNavController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            
        listsNavController.view.layoutIfNeeded()
    }
}

一切正常。但是,我想在CameraViewController处于活动状态时隐藏状态栏,而在listsNavController处于活动状态时显示状态栏。我正在这样做:

enum ViewControllerType {
    case camera
    case lists
}

class MasterViewController {
    var currentViewControllerType = ViewControllerType.camera
    override var prefersStatusBarHidden: Bool {
        if currentViewControllerType == .camera {
            return true
        } else {
            return false
        }
    }

    func goToLists() {
        listsNavController.view.frame.origin.x = view.frame.width
        UIView.animate(withDuration: 1, animations: {
            self.listsNavController.view.frame.origin.x -= self.view.frame.width
        }) { _ in

            /// on completion, I show the status bar
            self.currentViewControllerType = .lists
            self.setNeedsStatusBarAppearanceUpdate()
            
            self.listsNavController.didMove(toParent: self)
        }
    }
    func goToCamera() {
        /// I hide the status bar just before starting the animation
        self.currentViewControllerType = .camera
        self.setNeedsStatusBarAppearanceUpdate()
        
        listsNavController.view.frame.origin.x = 0
        UIView.animate(withDuration: 1, animations: {
            self.listsNavController.view.frame.origin.x += self.view.frame.width
        }) { _ in
            self.cameraViewController.didMove(toParent: self)
        }
    }
}

这就是它的样子(从cameraViewControllerlistsNavController然后返回):

going from "camera View Controller" to "lists Nav Controller" and back

问题在于,当我显示状态栏时,导航栏及其下的所有内容都会向下跳转。 (为使gif保持较小,我没有显示出该条下面的内容,但是所有内容都跳了。)此外,当我返回(从listsNavControllercameraViewController)时,导航栏的标题会设置动画,但是所有内容下方仍然跳动。

是否有办法使该跳跃动画化,从而使其平滑?还是最好根本不跳(隐藏状态栏时在导航栏上方有某种“占位符”)?

先谢谢了。如果需要,我可以提供更多代码。

0 个答案:

没有答案