iOS 10 UITabBar more items visible after setting title

随声附和 提交于 2019-12-05 00:48:35

This happens anytime you programmatically set a tab's Title in iOS 10. I have confirmed that this is a bug in iOS 10 beta. I have opened a bug report and radar: openradar.appspot.com/27749026

I have also posted a Github repo that demonstrates the issue: https://github.com/RippleAdder/TabStacks

This answer will not solve your particular case but could help others with the same problem.

The problem seems to appear if you're using the UIViewControllers tabBarItem property to set the title. Using the UITabBarController tabBar property instead should resolve the problem.

So instead of (e.g.):

tabBarController.viewControllers[0].tabBarItem.title = @"SomeText";

use:

tabBarController.tabBar.items[0].title = @"SomeText";

Update

We found out that replacing the UITabBarItem is another workaround to this iOS 10 bug:

UITabBarItem *item = tabBarController.viewControllers[0].tabBarItem;
tabBarController.viewControllers[0].tabBarItem = [[UITabBarItem alloc] initWithTitle:@"SomeText" image:item.image tag:item.tag];

A problem I had was in a view controller setting a title with self.title = @"Title" would cause the tabBarItem title to change and cause the ugly bug in this question. I changed it to self.navigationItem.title = @"Title" This seemed to fix the problem.

I've spend more time on this that I like to think about.

But the workaround that fixed for me was to surround the code in which I create and actually present the UITabBarItems on the main thread with a simple dispatch_async

dispatch_async(dispatch_get_main_queue(), {
    //your code here
}

As I said, might not be the best solution, but in the end was the one that actually worked.

Hope that Apple will fix this soon.

This bug still happens on iOS 11.3.

In my app i had to keep the current navigation bar title and current tab title in sync, so only this solution worked:

self.navigationItem.title = "Title"
self.tabBarController?.tabBar.items![0].title = self.navigationItem.title

Thanks @shady for the idea!

I am calling this method each time i want to update the title in both nav bar and tab bar (when user changes app language)

Move

self.title = @"SomeText";

from

- (void)awakeFromNib {}

to

- (void)viewDidLoad {}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!