UIImagePickerController inside UIPopoverController

2020-04-18 08:44发布

问题:

I'm using UIImagePickerController inside a UIPopoverController to select image just from photo albums. When I launch app on device running iOS 8, the Cancel button on the top right of the pop over appeared normally like this:

But when I launch the app on device running iOS 7, the Cancel button disappeared:

The code I used to show the picker:

    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
    [pickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    pickerController.delegate = self;

    _popOver = [[UIPopoverController alloc] initWithContentViewController:pickerController];
    _popOver.delegate = self;

    pickerController.navigationBar.tintColor = [UIColor redColor];//Cancel button text color
    [pickerController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];// title color

    if (isImage) {
        [pickerController setMediaTypes:@[(NSString*)kUTTypeImage]];
    } else
        [pickerController setMediaTypes:@[(NSString*)kUTTypeMovie]];
    [_popOver presentPopoverFromRect:CGRectMake(1024/2, 768/2, 1, 1) inView:self.view permittedArrowDirections:0 animated:YES];

What can I do to show that Cancel button on iOS7? My app design doesn't allow user to dismiss the popover by tapping anywhere outside the popover view.

Thank you.

回答1:

@JozoL Write the Below Code this works

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

        UINavigationItem *pickerNavBarTopItem;
        // add done button to right side of nav bar
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"
                                                                       style:UIBarButtonItemStylePlain
                                                                      target:self
                                                                      action:@selector(doSomething)];

        UINavigationBar *bar = navigationController.navigationBar;
        [bar setHidden:NO];
        pickerNavBarTopItem = bar.topItem;
        pickerNavBarTopItem.rightBarButtonItem = doneButton;
    }
    -(void)doSomething{

    }


回答2:

For swift2.x you can try below code, Its work for me

pickerController.navigationBar.tintColor = UIColor.blueColor()



回答3:

In my case, UIImagePickerController was not showing its cancel button . So I tried adding this line to code:

pickerController.navigationBar.barStyle = UIBarStyleDefault;

And this worked for me.Try setting color of Cancel button like this

 pickerController.navigationBar.tintColor = [UIColor blackColor];

Try using this code and let me know if it helps you.