UIPopoverController中的UIImagePickerController在iOS7上没有显示“取消”按钮

时间:2014-11-14 03:35:23

标签: ios ios7 ios8 uiimagepickercontroller uipopovercontroller

我在UIPopoverController中使用UIImagePickerController来从相册中选择图像。当我在运行iOS 8的设备上启动应用程序时,弹出窗口右上角的“取消”按钮通常会显示如下:

enter image description here

但是当我在运行iOS 7的设备上启动应用程序时,“取消”按钮消失了: enter image description here

我用来显示选择器的代码:

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

如何在iOS7上显示“取消”按钮?我的应用程序设计不允许用户通过点击弹出窗口视图外的任何位置来关闭弹出窗口。

谢谢。

3 个答案:

答案 0 :(得分:2)

@JozoL编写以下代码

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

    }

答案 1 :(得分:1)

对于swift2.x,您可以尝试下面的代码,它适用于我

pub

答案 2 :(得分:0)

就我而言,UIImagePickerController没有显示取消按钮 。所以我尝试将这一行添加到代码中:

pickerController.navigationBar.barStyle = UIBarStyleDefault;

这对我有用。尝试像这样设置取消按钮的颜色

 pickerController.navigationBar.tintColor = [UIColor blackColor];

尝试使用此代码并告诉我它是否对您有帮助。