问题
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 :)
I'd like to change the case of a UIImagePickerController's navigation bar items.
This works for the title text:
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).
How can I change the cancel button too?
[viewController.navigationItem.rightBarButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem* btn, NSUInteger idx, BOOL *stop) {
btn.title = [btn.title lowercaseString];
}];
回答1:
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;
}
EDIT:
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];
}
}
}
来源:https://stackoverflow.com/questions/27282497/change-case-of-uiimagepickercontroller-navigation-bar-items