简体   繁体   English

如何在UIImagePickerController中更改取消按钮标题?

[英]How to change cancel button title in UIImagePickerController?

Currently I'm working on a multi-language application and I want to change the Cancel , Use and Retake button titles of the UIImagePickerController . 目前我正在开发一个多语言应用程序,我想更改UIImagePickerControllerCancelUseRetake按钮标题。 How can I do that? 我怎样才能做到这一点?

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];

Just add needed localizations in your project: 只需在项目中添加所需的本地化:
1) Select project file; 1)选择工程文件;
2) Select your project in "Project and Targets" list; 2)在“项目和目标”列表中选择项目;
3) Select "Info" (in top part of screen); 3)选择“信息”(在屏幕顶部);
4) Add needed languages in "Localizations" section. 4)在“本地化”部分中添加所需的语言。

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. 诸如MFMessageComposerMFMailComposerUIImagePicker等控件。将默认控件文本自动更改为设备语言。

Assign delegate as self to your imagepicker controller and add the following code : 将委托作为self分配给imagepicker控制器并添加以下代码:

- (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;
}

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];
}

Just find and change text of UIButton 只需查找并更改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];
                }

            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM