【问题标题】:UINavigation bar showing a weird white space in place of text label when UIImagePickerController presented当 UIImagePickerController 出现时,导航栏显示一个奇怪的空白代替文本标签
【发布时间】:2017-12-10 09:57:15
【问题描述】:

我有一个视图,我想根据用户的选择展示 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];

这是我尝试从照片(图库)中提取时的图像

这张图片是我尝试打开相机的时候。

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

【问题讨论】:

  • 您是否尝试过使用 [self presentViewController:picker animated:YES completion:nil] 呈现 UIImagePickerController。请删除 self.delegate 并检查。
  • 不,它不起作用。

标签: ios objective-c uiimagepickercontroller


【解决方案1】:

这可能是因为 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];

很酷,您可以通过这个容器控制器将选择器包含到情节提要流程中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 2011-08-19
    • 1970-01-01
    • 2020-10-01
    • 2020-03-12
    • 2020-07-17
    • 1970-01-01
    相关资源
    最近更新 更多