Why is self.navigationItem.hidesBackButton not wor

2020-07-10 04:56发布

I have a UIViewController that is pushed onto a UINavigationController and is currently displayed. When I go to start some asynchronous task inside the view controller, I can set hidesBackButton on self.navigationItem to YES, and the back button is hidden correctly.

As soon as the task is finished, and I set hidesBackButton back to NO (on the UI thread, I might add, I've made sure of this), nothing happens. The back button remains hidden.

Has anyone seen this before? What drives me especially crazy is that in my application (the same application), in a different UINavigationController hierarchy, the exact same code works correctly!

6条回答
叛逆
2楼-- · 2020-07-10 05:33

i have not been able to replicate your problem on my machine. however, i faced a similar issue with tableviews even when i was updating my ui on the main thread. but calling setNeedsDisplay fixed that issue.

Can you try this and see if this works:

[self.navigationController.navigationBar setNeedsDisplay];

I guess this should work, you need to do the same, BUT ON THE NAVIGATIONBAR instead. please let me know if this worked - as i cannot test my solution because i never get this problem :-)

查看更多
迷人小祖宗
3楼-- · 2020-07-10 05:48

I have had a similar issue recently. I tried literally everything I found in SO and other forums- nothing worked.

In my case there was a modally shown UINavigationController with a simple root controller which would push one of two view controllers (A and B) on top of the controller stack when the button A or B was pressed, respectively. Controller B was the one which was not supposed to show the back button. But still, sometimes it did, sometimes it didn't.

After hours of debugging, I managed to track it down. Controller A was a UITableViewController. Each time I selected a cell in this controller, the delegate would pop Controller A off the stack. BUT. I made use of a UISearchDisplayController as well. Turned out that popping the view while the search controller was still active messed up something in the navigation controller that made it impossible to hide the back button in Controller B afterwards (well, it eventually stayed hidden between viewDidLoad and viewDidAppear: but then it always turned visible).

So the solution (rather workaround) was adding this line to where Controller A was dismissed:

controllerA.searchDisplayController.active = NO;

// ...
// [self.navigationController popViewControllerAnimated:YES];

Hope this spares someone a couple of hours.

查看更多
祖国的老花朵
4楼-- · 2020-07-10 05:52

In my case I simply had to give a title to the view, as in: self.navigationItem.title = @"Menu";

Marinus

查看更多
女痞
5楼-- · 2020-07-10 05:54

Have you tried forcing the view to refresh by calling setNeedsDisplay? Maybe the OS is not picking up the changes instantly and you need to force it.

查看更多
时光不老,我们不散
6楼-- · 2020-07-10 05:56

Have you tried using the setHidesBackButton:animated: method instead? Perhaps that has a slightly different behavior.

查看更多
冷血范
7楼-- · 2020-07-10 05:58

Are you calling hidesBackButton = NO from a thread? All UI operations should be done on the main thread, otherwise they won't have any effect.

查看更多
登录 后发表回答