【问题标题】:Custom Trimming Back button text of UINavigationControllerUINavigationController 的自定义修剪后退按钮文本
【发布时间】:2014-11-10 04:43:30
【问题描述】:
环境:
我正在为我的 iOS 应用开发应用内区域设置更改支持。
这比听起来要难,因为UIKit 提供的控件语言环境与 iOS 语言环境相关联。
我几乎可以解决问题,但现在还剩下一件事:
问题:
当我们使用 UINavigationController 推送视图控制器时。
它会自动生成带有前一个视图控制器的导航项标题的后退按钮项。
但是,如果标题太长而无法作为后退按钮显示,UINavigationController 使用 Back 作为后备标题。
自动缩短标题(后退)与系统语言环境相关。这是我的问题。
我可以合法地改变这种行为吗?
【问题讨论】:
标签:
ios
uinavigationcontroller
【解决方案1】:
我自定义了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];
}
我不能确定这是最好的解决方案,但是它是合法的并且很有魅力。