How to hide Navigation Bar in embedded UIImagePick

2019-08-25 07:05发布

问题:

I'm trying to present UIImagePickerController as a subview in ordinary UIViewController.

The problem I'm facing is that I'm unable to hide the inner navigation bar (with "Chwile" title and "Anuluj" button). It looks something like this:

I tried subclassing UIImagePickerController with my custom controller. I also tried to style the NavigationBar property and achieved the following result:

However, I'd like to remove the Navigation Bar entirely. I tried to call SetNavigationBarHidden(true, true); and NavigationController.SetNavigationBarHidden(true, true); in ViewWillAppear, ViewDidLoad and also ViewDidAppear of custom image picker controller, but with no result.

Do you know how to remove the nav bar entirely? The solution in swift or obj-c is also perfect for me.

    private void PresentImagePicker(UIImagePickerControllerSourceType type)
    {
        imagePicker = new CustomImagePickerController
        {
            SourceType = type,
            AllowsEditing = false,
        };

        imagePicker.FinishedPickingMedia += OnImagePickerFinishedPickingMedia;
        imagePicker.Canceled += OnImagePickerCancelled;

        AddChildViewController(imagePicker);

        ContentView.AddSubview(imagePicker.View);
        imagePicker.View.Frame = Content.Bounds;
        imagePicker.View.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;

        imagePicker.DidMoveToParentViewController(this);
    }

回答1:

Instead of adding subview to parent view and managing frames, you can simply use presentViewController like this:

self.present(imagePicker, animated: true, completion: nil)

So, you need to remove following these lines and replace them with just one line as above:

        AddChildViewController(imagePicker);
        ContentView.AddSubview(imagePicker.View);
        imagePicker.View.Frame = Content.Bounds;
        imagePicker.View.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;

        imagePicker.DidMoveToParentViewController(this);

and it will work fine