在导航栏中隐藏和显示后退按钮

Hide and show back button in navigationBar

我有这两个方法:

func showSpinner()
{
    let spinner = UIActivityIndicatorView(activityIndicatorStyle: .White)
    spinner.startAnimating()

    self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: spinner)
    self.navigationItem.hidesBackButton = true
}

func hideSpinner()
{
    self.navigationItem.leftBarButtonItem = nil
    self.navigationItem.hidesBackButton = false
}

viewDidLoad 中调用 showSpinner,然后在加载数据后调用 hideSpinner。但是backButton经常在hideSpinner上跳转。如何解决?

您可以在移除微调器后使用延迟来显示后退按钮。喜欢 0.2 秒或按要求

func hideSpinner()
{
    self.navigationItem.leftBarButtonItem = nil
    dispatch_after(1, dispatch_get_main_queue()) { () -> Void in
        self.navigationItem.hidesBackButton = false
    };
}