当UIImagePickerController呈现时,UINavigation栏显示一个奇怪的空格代替文本标签

时间:2017-12-10 09:57:16

标签: ios objective-c uiimagepickercontroller

我有一个视图,我想根据用户选择提供UIImagePicker相机或照片。但是当我展示我的选择器控制器时,我的导航栏是黑色的,出现了一个奇怪的白色背景而不是标签。

用于相机

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            [[picker navigationBar] setTintColor:[UIColor whiteColor]];


            picker.modalPresentationStyle = UIModalPresentationCurrentContext;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.showsCameraControls = YES;
            picker.delegate = self;
            //[picker setAllowsEditing:YES];
            [self.delegate presentViewController:picker animated:YES completion:nil];

来自相册的照片选择器

 UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            [[picker navigationBar] setTintColor:[UIColor whiteColor]];


//            picker.modalPresentationStyle = UIModalPresentationCurrentContext;
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            //picker.showsCameraControls = YES;
            picker.delegate = self;
            //[picker setAllowsEditing:YES];
            [self.delegate presentViewController:picker animated:YES completion:nil];

这是我尝试从照片(图库)中选择的图像 enter image description here

这张图片是我试图打开相机的时候。 enter image description here

他们有这个奇怪的导航栏,当我们提出一个pickerViewController时,我猜通常会被ImagePicker取代。

1 个答案:

答案 0 :(得分:0)

可能会发生这种情况,因为UIImagePickerController是UINavigationController的子类,它有自己的控制器堆栈(以及它自己的导航栏)。

一个棘手的解决方案是使用容器控制器: 在容器控制器init方法中创建选择器:

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _picker =  [UIImagePickerController new];
        _picker.delegate = self;
        _picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
            _picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        }
        _picker.mediaTypes = @[(__bridge NSString *)kUTTypeImage];
        _picker.delegate = self;
    }
    return self;
}

将选择器添加为子vc并隐藏它的导航栏:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.blackColor;
    [self.picker setNavigationBarHidden:YES];

    [self addChildViewController:self.picker];
    [self.picker didMoveToParentViewController:self];
    [self.view addSubview:self.picker.view];
}

布局它的框架:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    CGFloat kNavbarHeight = 64;
    self.picker.view.frame = CGRectMake(0, kNavbarHeight, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - kNavbarHeight);
}

现在你可以推送或模态呈现容器:

UIViewController *vc = [EHPickerContainerController new];
vc.title = @"Test";

[self.navigationController pushViewController:vc animated:YES];

enter image description here

很酷,你可以通过这个容器控制器将一个选择器包含在故事板流中。