UIStatusBarStyleBlackTranslucent is not available

2019-04-30 03:35发布

问题:

I have a UIActionSheet for iPad which has three options :

  1. Cancel
  2. Camera
  3. Photo Library

When I touch the "Photo Library" option I get a crash and a message

UIStatusBarStyleBlackTranslucent is not available on this device.

I read this post, but didn't figure it out.

Can someone help me?

Update :

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

I get crash in last line i.e [self presentModalViewController:imgController animated:YES];

回答1:

For iPad it is recommended that you should use popover to present the MediaBrowser (camera / photoLibrary):

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

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

You can also set the content view for 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:

Try below code its works for me perfect

- (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:

Try removing your status bar settings from the plist file all together and adding the following to your AppDelegate's applicationDidFinishLaunchingWithOptions:

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

UPDATE:

Try this

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


回答4:

A little late but is the UIViewController whats calling presentModalViewController:animated: a child of a UIPopoverController? If so, thats whats causing this. Try calling it from the popovers parentViewController



回答5:

Just as you pointed out with the post that you have read, the simple solution would be adding a row in the plist with the following key-value

UIStatusBarStyle~ipad | String | UIStatusBarStyleBlackOpaque

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

This is one of the solution if you don't want to do too much "dirty work" on the codes there, just leave it to the plist to get the job done.

But if you don't mind writing codes, the solution given by VSN will do the same as my suggestion.