ChikabuZ
ChikabuZ

Reputation: 10185

Hide and show back button in navigationBar

I have these two methods:

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
}

In viewDidLoad I call showSpinner and then after data loaded I call hideSpinner. But backButton often jumping on hideSpinner. How to fix it?

enter image description here

Upvotes: 1

Views: 2403

Answers (1)

Vijay Masiwal
Vijay Masiwal

Reputation: 1153

You can use delay to show the back button after removing the spinner. Like 0.2 seconds or as per the requirement

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

Upvotes: 4

Related Questions