UINavigationController的自定义修剪后退按钮文本

时间:2014-11-10 04:43:30

标签: ios uinavigationcontroller

环境

我正在开发针对iOS应用的应用内语言环境更改支持。 这听起来很难,因为UIKit提供的控件的区域设置与iOS Locale绑定。

我可以解决几乎所有的问题,但现在仍有一件事情要做:

问题

当我们使用UINavigationController推送视图控制器时。 它将自动生成带有前一个视图控制器的导航项标题的后退按钮项。

但是,如果标题太长而无法显示为后退按钮,UINavigationController会使用Back作为后备标题。 自动缩短标题(Back)与系统区域设置相关联。这是我的问题。

我可以合法更改此行为吗?

1 个答案:

答案 0 :(得分:0)

我制作了自定义UINavigationController并覆盖了pushViewController:animated:

-(void)pushViewController:(UIViewController *)viewController
                 animated:(BOOL)animated
{
    UINavigationItem* current = self.topViewController.navigationItem;

    if(current.backBarButtonItem == nil){
        current.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:current.title style:UIBarButtonItemStylePlain target:nil action:nil];
    }

    UIBarButtonItem* backItem = current.backBarButtonItem;
    CGFloat width = [backItem.title sizeWithAttributes:@{}].width;

    // Trimming to localized back text
    if(width > 70){
        backItem.title = [[KKCoreLocale shared] localizedStringForKey:@"Back"];
    }

    [super pushViewController:viewController animated:animated];
}

我无法确定这是最好的解决方案,但它是合法的并且有魅力。

相关问题