When changing UIBarButtonItem title, the transition is jerky/flickers

本小妞迷上赌 提交于 2019-12-10 01:23:59

问题


I have a UIViewController subclass. I give the view controller a rightBarButtonItem in viewDidLoad like this:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Difficulty"
      style:UIBarButtonItemStylePlain
      target:nil
      action:nil];

When the user presses another button in the view, the title/text of the UIBarButtonItem is changed like this:

self.navigationItem.rightBarButtonItem.title = @"Mellansvår";

It works fine, but when the title is changed, it doesn't look very good. The text jumps around a bit.

I have changed UILabel text at runtime before, and when I change their text is doesn't look like this, but I don't know how to add a UILabel to the navigationItem. I can't do it in Interface Builder because the navigation bar comes from a UINavigationController.

Is there a way to change the title more smoothly?


回答1:


Instead of setting the title again, you could set the button again with the title and then animate it:

(void)setRightBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated



回答2:


Another solution would be to change the title without animations.

[UIView performWithoutAnimation:^{
    self.navigationItem.rightBarButtonItem.title = @"Mellansvår";
}];



回答3:


You can skip the animation altogether, if you change the UIBarButtonItem title while it is not attached to the navigationItem, like this:

UIBarButtonItem *tempItem = self.navigationItem.rightBarButtonItem;
self.navigationItem.rightBarButtonItem = nil;
tempItem.title = @"Mellansvår";
self.navigationItem.rightBarButtonItem = tempItem;

The animation will disappear completely, but the visual effect looks way better IMHO.



来源:https://stackoverflow.com/questions/23307907/when-changing-uibarbuttonitem-title-the-transition-is-jerky-flickers

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