1

鉴于以下代码,按钮不会显示在工具栏上。有什么不对?

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

理论上,那些 cancelBarButtons 和其他应该出现在工具栏上。但是,在这种情况下不会。请指教。

4

3 回答 3

3

为了那些搜索和找到这篇文章的人的利益,迟到的答案。

在 iOS 5 及更高版本中,解决方案是在完成块中调用presentViewController:animated:completion和设置工具栏项。

    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];
    NSArray *tbItems = [NSArray arrayWithObjects:cancelBarButtonItem,flexibleBarSpace,cameraBarButtonItem,flexibleBarSpace,faceBarButtonItem,nil];

    // [self presentModalViewController:cameraPickerController animated:YES];
    cameraPickerController.modalTransitionStyle = UIModalPresentationFullScreen;
    [self presentViewController:picker animated:YES completion:^{
        cameraPickerController.toolbar.items = tbItems;
    }];
于 2013-05-10T14:31:59.467 回答
3

您不能将项目添加到工具栏,因为它还没有被UIImagePickerController(我认为)呈现。而是试试这个:

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

基本上我只是创建了一个新工具栏并将其设置为cameraOverlayView.

干杯!

于 2012-07-24T20:37:50.933 回答
0

所以我搜索了类似的问题,发现了这个

但基本上他的问题是他正在尝试

[self presentModalViewController:foo animated:YES];

修复是

[self.tabBarController presentModalViewController:camera animated:YES];

看看这是否也能解决你的问题。如果没有看到更多代码,很难确定

于 2012-07-24T19:00:51.053 回答