How to change cancel button title in UIImagePickerController?

微笑、不失礼 提交于 2019-12-01 09:52:09

问题


Currently I'm working on a multi-language application and I want to change the Cancel ,Use and Retake button titles of the UIImagePickerController. How can I do that?


回答1:


My problem is solved by using custom overlay class.

self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
self.picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
self.picker.showsCameraControls = NO;
self.picker.navigationBarHidden = YES;
self.picker.toolbarHidden = YES;
self.picker.wantsFullScreenLayout = YES;

// Insert the overlay
self.overlay = [[OverlayViewController alloc] initWithNibName:@"Overlay" bundle:nil];
self.overlay.pickerReference = self.picker;
self.picker.cameraOverlayView = self.overlay.view;
self.picker.delegate = self.overlay;

[self presentModalViewController:self.picker animated:NO];



回答2:


Just add needed localizations in your project:
1) Select project file;
2) Select your project in "Project and Targets" list;
3) Select "Info" (in top part of screen);
4) Add needed languages in "Localizations" section.




回答3:


It'll be automatically changed to device language.

You don't need o worry about this. Also you can't change it's behavior.

Controls like: MFMessageComposer, MFMailComposer, UIImagePicker etc. Will change it's default controls text to device language automatically.




回答4:


Assign delegate as self to your imagepicker controller and add the following code :

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

     UINavigationItem *ipcNavBarTopItem;

    // add done button to right side of nav bar
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@\"Done\"
                            style:UIBarButtonItemStylePlain 
                            target:self 
                            action:@selector(saveImages:)];

    UINavigationBar *bar = navigationController.navigationBar;
   [bar setHidden:NO];
       ipcNavBarTopItem = bar.topItem;
       ipcNavBarTopItem.title = @\"Pick Images\";
   ipcNavBarTopItem.rightBarButtonItem = doneButton;
}



回答5:


Only good solution is here...

//this is your camera method
- (void)startCameraControllerUsingDelegate:(id<UIImagePickerControllerDelegate, UINavigationControllerDelegate>)delegate gallery:(BOOL)openGallery
{
    if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)
        || (delegate == nil))
        return;

    UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
    if (openGallery) {
        cameraUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    } else {
        cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
    }

    cameraUI.allowsEditing = YES;
    //set your delegate type UINavigationControllerDelegate here! It will trigger function bellow!
    cameraUI.delegate = delegate;

    [self presentViewController:cameraUI animated:YES completion:nil];
}

//UINavigationControllerDelegate function
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //this is where magic happends! This leaves button empty and it will not ruin title center position!... or give it your desired string
    viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}



回答6:


Just find and change text of UIButton

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
        for (UIView *v in viewController.view.subviews)
        {
            if ([v isKindOfClass:[NSClassFromString(@"CMKBottomBar") class]])
            {
                SEL se = NSSelectorFromString(@"cancelButton");

                if ([v respondsToSelector:se])
                {
                    UIButton *cancelButton =  [v performSelector:se];
                    [cancelButton setTitle:@"New title" forState:UIControlStateNormal];
                }

            }
        }
    }


来源:https://stackoverflow.com/questions/16436495/how-to-change-cancel-button-title-in-uiimagepickercontroller

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