Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}
like image 863
Shri Avatar asked Sep 09 '10 10:09

Shri


2 Answers

[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.

like image 118
Jano Avatar answered Oct 03 '22 16:10

Jano


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!

like image 32
gpiazzese Avatar answered Oct 03 '22 14:10

gpiazzese