Shri
Shri

Reputation: 2139

How to add UIImagePickerController as a sub view instead of Modal View

I have a segment controller on one of my views and now on the 0th index of segment controller I want to add UIImagePickerController (for showing camera view to user) by adding as sub view and not by ModalViewController. Right now the view gets loaded but It does not show any camera view. I am able to show the camera view by presentModalViewController and passing its object.

Here's the code--

if(segmentedControl.selectedSegmentIndex==0)

{

UIImagePickerController *cameraView = [[UIImagePickerController alloc] init];

cameraView.sourceType = UIImagePickerControllerSourceTypeCamera;

cameraView.showsCameraControls = NO;

//[self presentModalViewController:cameraView animated:YES]; //Working

[self.view addSubview:cameraView.view]; // Not Working

}

Upvotes: 6

Views: 5954

Answers (2)

gpiazzese
gpiazzese

Reputation: 445

You should avoid doing this, it is not recommended and could lead to unwanted side effects.

As stated on the documentation (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImagePickerController_Class) you cannot add it as a subview, you should present it as a new controller

Here a piece of the doc:

On iPhone or iPod touch, do this modally (full-screen) by calling the presentViewController:animated:completion: method of the currently active view controller, passing your configured image picker controller as the new view controller.

Hope this helpes!

Upvotes: 1

Jano
Jano

Reputation: 63707

[self.view addSubview:picker.view];
[picker viewWillAppear:YES]; // trickery to make it show
[picker viewDidAppear:YES];

You get a white bar at the top as side effect since UIImagePickerController is not intended to be used with it.

Upvotes: 11

Related Questions