Change case of UIImagePickerController navigation bar items

二次信任 提交于 2019-12-12 04:34:08

问题


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

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