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

问题描述:

目前我正在开发多语言应用程序,并且想要更改UIImagePickerControllerCancelUseRetake按钮标题。我怎样才能做到这一点?如何更改UIImagePickerController中的取消按钮标题?

我的问题是通过使用自定义覆盖类解决。

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

GUD解决方案亲爱的... – Romance 2013-05-08 12:16:21

它会自动更改为设备语言。

你不需要担心这一点。你也不能改变它的行为。

控件像:MFMessageComposerMFMailComposer,UIImagePicker等将自动将其默认控件文本更改为设备语言。

+0

如果设备语言是英语,我想在那个时候改变标题,我们如何实现这一目标? – Balu 2013-05-08 09:13:55

+0

@孙:不可能。 – 2013-05-08 09:30:55

指派代表作为自我的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; 
} 

只需添加所需的本地化项目中的:
1)选择工程文件;
2)在“项目和目标”列表中选择您的项目;
3)选择“信息”(在屏幕顶部);
4)在“本地化”部分添加所需的语言。

只要找到和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]; 
       } 

      } 
     } 
    } 

的变化文本这一切都是一堆垃圾。只有好的解决方案在这里...

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