3
votes

Strange problem with the navigation bar and UIToolbar when they are on the same view on the same time. When swiping back the vc to return the firstvc on the screen, the strange gray box appears on the navigation bar:

Image

Animation

UIViewController *firstvc = [[UIViewController alloc] init];
firstvc.view.backgroundColor = [UIColor whiteColor];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 523, 320, 45)];
[firstvc.view addSubview:toolbar];

UIViewController *secondvc = [[UIViewController alloc] init];
secondvc.view.backgroundColor = [UIColor whiteColor];

UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:firstvc];
[nvc pushViewController:secondvc animated:NO];

Everything works as expected when I change the translucent property for the toolbar, but I do not like this solution – UIToolbar is intended to be translucent. I believe it is a common way to use UIToolbar on views with UINavigationController. What did I do wrong?

UPDATE: There is a great property of UINavigationController called toolbar, which add the toolbar on the bottom of the screen. However, the question mostly about how to use UIToolbar and UINavigationController together. For example, I developed a complicated view with the toolbar on the top of the screen, which included a custom UITextField. When user selected something on the view, another view controller, which had a native navigation bar, popped up. In this particular case I had the same problem and had no other choice but disable the translucent property for the toolbar of the first view controller.

Here the animation that can express my idea.

And again the same gray box.

1

1 Answers

1
votes

You don't have to create toolbar and add it. UIViewController already has a toolbar which you can use without this issues that you described.