Search code examples
iosipaduiimagepickercontrolleruitoolbar

iOS: UIImagePickerController - using toolbar property?


Given the following code, The buttons does not show up on the toolbar. Is there something wrong?

UIImagePickerController* cameraPickerController = [[UIImagePickerController alloc] init];
cameraPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPickerController.delegate =self;
cameraPickerController.showsCameraControls = NO;
cameraPickerController.toolbarHidden = NO;

UIBarButtonItem *cancelBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target: self action: @selector(cancel)];
UIBarButtonItem *flexibleBarSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
flexibleBarSpace.width = 1000;
UIBarButtonItem *cameraBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:cameraPickerController action:@selector(takePicture)];
UIBarButtonItem *faceBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Show facemask" style:UIBarButtonItemStyleBordered target: self action: @selector(showFacemask)];
cameraPickerController.toolbar.items = [NSArray arrayWithObjects:cancelBarButtonItem,flexibleBarSpace,cameraBarButtonItem,flexibleBarSpace,faceBarButtonItem,nil];

[self presentModalViewController:cameraPickerController animated:YES];

Those cancelBarButtons and other should in theory show up on the toolbar. However, it does not in this case. Please advise.


Solution

  • You can not add items to the toolbar because it has not been presented yet by the UIImagePickerController (I think.) Instead try this:

    UIImagePickerController* cameraPickerController = [[UIImagePickerController alloc] init];
    cameraPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    cameraPickerController.delegate =self;
    cameraPickerController.showsCameraControls = NO;
    
    UIToolbar *theToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 436, 320, 44)];
    
    UIBarButtonItem *cancelBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target: self action: @selector(cancel)];
    UIBarButtonItem *flexibleBarSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    flexibleBarSpace.width = 1000;
    UIBarButtonItem *cameraBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:cameraPickerController action:@selector(takePicture)];
    UIBarButtonItem *faceBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Show facemask" style:UIBarButtonItemStyleBordered target: self action: @selector(showFacemask)];
    
    theToolbar.items = [NSArray arrayWithObjects:cancelBarButtonItem, flexibleBarSpace, cameraBarButtonItem, faceBarButtonItem, nil];
    
    [cameraPickerController setCameraOverlayView:theToolbar];
    
    [self presentModalViewController:cameraPickerController animated:YES];
    

    Basically I just created a new toolbar and set it up as the cameraOverlayView.

    Cheers!