【问题标题】:UIStatusBarStyleBlackTranslucent is not available on this deviceUIStatusBarStyleBlackTranslucent 在此设备上不可用
【发布时间】:2026-02-22 06:30:02
【问题描述】:

我有一个用于 iPad 的UIActionSheet,它有三个选项:

  1. 取消
  2. 相机
  3. 图片库

当我触摸“照片库”选项时,我收到了崩溃和一条消息

UIStatusBarStyleBlackTranslucent 在此设备上不可用。

我读了this post,但没弄明白。

有人可以帮助我吗?

更新

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 0) 
    {

        imgController = [[UIImagePickerController alloc] init];
        imgController.allowsEditing = YES;
        imgController.sourceType =  UIImagePickerControllerSourceTypeCamera;   
        imgController.delegate=self;
        [self presentModalViewController:imgController animated:YES];

    } 
    else if (buttonIndex == 1) 
    {
        imgController = [[UIImagePickerController alloc] init];
        imgController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
        imgController.delegate=self;
        [self presentModalViewController:imgController animated:YES];  
}
}  

我在最后一行遇到崩溃,即[self presentModalViewController:imgController animated:YES];

【问题讨论】:

  • 对不起。错过了。现在编辑。请检查。

标签: ios cocoa-touch ipad ios5 uistatusbar


【解决方案1】:

对于 iPad,建议您使用 popover 来呈现 MediaBrowser(相机/照片库):

UIImagePickerController *ipc = [[UIImagePickerController alloc] init];

UIPopoverController *popOverController = [[UIPopoverController alloc] initWithContentViewController:ipc];
popOverController.delegate = self;

你也可以设置popover的内容视图:

ipc.delegate = self; 
ipc.editing = NO;       
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
ipc.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:ipc.sourceType];

[popOverController presentPopoverFromRect:btnGallery.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

【讨论】:

    【解决方案2】:

    试试下面的代码,它对我来说很完美

    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:   (NSInteger)buttonIndex
    {
    if(buttonIndex==0)
        {
        [self takePhoto];
    
        }
    else if(buttonIndex==1)
        {
        [self choosePhoto];
        }
    }
    
    
    -(void)takePhoto
    {
    UIDevice *device = [UIDevice currentDevice];
    
    NSString *currDevice = [device model];
    
    NSLog(@"device is %@",currDevice);
    
       if(![currDevice isEqualToString:@"iPhone Simulator"])
            {
           [[UIApplication sharedApplication]    setStatusBarOrientation:UIInterfaceOrientationPortrait];
        UIImagePickerController *imgPickerCon = [[UIImagePickerController alloc] init];
        imgPickerCon.sourceType = UIImagePickerControllerSourceTypeCamera;
        imgPickerCon.delegate = self;
        [self presentModalViewController:imgPickerCon animated:YES];
        [imgPickerCon release];
        imgPickerCon = nil;
        }
      else{
        UIAlertView *alrt=[[UIAlertView alloc] initWithTitle:@"Message" message:@"Camera Will Not Open in Simulator" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alrt show];
        [alrt release];
    }
     }
    
     -(void)choosePhoto
     {
    
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
    UIImagePickerController *imgPickerCon = [[UIImagePickerController alloc] init];     
    imgPickerCon.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imgPickerCon.delegate = self;
    [self presentModalViewController:imgPickerCon animated:YES];        
    [imgPickerCon release];
    imgPickerCon = nil;
      }
    
    
      - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissModalViewControllerAnimated:YES];
     }
    
       - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)myimage editingInfo:(NSDictionary *)editingInfo 
      {
    
    
    [picker dismissModalViewControllerAnimated:YES];
    image=myimage;
    
    imgView.image=myimage;
    
      }
    

    【讨论】:

      【解决方案3】:

      尝试从plist 文件中删除您的状态栏设置,并将以下内容添加到您的AppDelegate's applicationDidFinishLaunchingWithOptions:

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
       [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackTranslucent];
      }
      

      更新:

      试试这个

      -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
      switch (buttonIndex)
      {
          case 0: 
          {
      
              imgController = [[UIImagePickerController alloc] init];
              imgController.allowsEditing = YES;
              imgController.sourceType =  UIImagePickerControllerSourceTypeCamera;   
              imgController.delegate=self;
              [self presentModalViewController:imgController animated:YES];
      
          } 
          case 1:
          {
              imgController = [[UIImagePickerController alloc] init];
              imgController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
              imgController.delegate=self;
              [self presentModalViewController:imgController animated:YES];  
          }
      }
      } 
      

      【讨论】:

      • 检查更新,这是我立即看到的唯一一件我会做不同的事情。
      • 老实说真的没什么,但尝试一下也无妨。您的代码似乎没有任何问题。
      • 不,我问的是你做了什么改变?
      • 如果我添加了一个开关盒,而不是你的 if/else。试着用我写的替换你写的所有东西。它从您的代码中进行编辑,因此不会有任何问题。
      【解决方案4】:

      有点晚了,但是 UIViewController 是什么在调用 presentModalViewController:animated: 一个 UIPopoverController 的孩子吗?如果是这样,那就是造成这种情况的原因。尝试从弹出框 parentViewController 调用它

      【讨论】:

        【解决方案5】:

        正如您在阅读过的the post 中指出的那样,简单的解决方案是在 plist 中添加一行,其中包含以下键值

        UIStatusBarStyle~ipad |字符串 | UIStatusBarStyleBlackOpaque

        (3rd row in the picture here, sorry cause I can't post image yet now)

        如果您不想对那里的代码做太多“肮脏的工作”,这是解决方案之一,只需将其留给 plist 即可完成工作。

        但如果你不介意写代码,VSN 给出的解决方案会和我的建议一样。

        【讨论】:

          最近更新 更多