问题描述
免责声明:我意识到这可能不会得到 Apple 的批准,并且我意识到遍历视图层次结构在编程上是不安全的.我正出于自己的好奇心想弄清楚:)
Disclaimer: I realize this may not be approved by Apple, and I realize traversing the view hierarchy is programmatically unsafe. I'm trying to figure it out for my own curiosity :)
我想更改 UIImagePickerController 导航栏项的大小写.
I'd like to change the case of a UIImagePickerController's navigation bar items.
这适用于标题文本:
viewController.navigationItem.title = [viewController.navigationItem.title uppercaseString];
但这对取消按钮不起作用(显然它不是取消按钮的正确位置,但我在视图层次结构中找不到它).
But this doesn't work for the cancel button (clearly it's not the right location for the cancel button, but I can't find it in the view hierarchy).
如何更改取消按钮?
[viewController.navigationItem.rightBarButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem* btn, NSUInteger idx, BOOL *stop) {
btn.title = [btn.title lowercaseString];
}];
推荐答案
我不知道如何访问现有的取消按钮,但您可以通过以下方式替换它:
I don't know how to access the existing cancel button, but you can replace it doing something like this:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// add done button to right side of nav bar
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"cancel"
style:UIBarButtonItemStylePlain
target:self
action:@selector(done:)];
UINavigationBar *bar = navigationController.navigationBar;
UINavigationItem *topItem;
topItem = bar.topItem;
topItem.rightBarButtonItem = cancelButton;
}
这会做你想做的,我想:
This will do what you want, I think:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
for (UIView *view in navigationController.navigationBar.subviews)
{
if ([view isKindOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)view;
[btn setTitle:[btn.titleLabel.text lowercaseString] forState:UIControlStateNormal];
}
}
}
这篇关于UIImagePickerController 导航栏项的更改案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!