iPad:Lock UIView orientation into Landscape mode only
This requirement is for iPad. I have an UIView which used as Camera overlay view when launching video recording. It set in UIImagePickerController as below.
[self presentModalViewController:pickerController animated:NO];
pickerController.cameraOverlayView =myOwnOverlay;
This is my requirement that i have to provide my own overlay in UIImagePickerController when calling camera for video recording. I want to lock my own camera overlay UIView into LANDSCAPE mode only, so that allow user can record video in Landscape mode only and not in Portrait mode, this is also my project requirement. I know about shouldAutorotateToInterfaceOrientation which is used for UIViewController. I used "[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];" it, it locks to Landscape mode when i launch this camera own overlay UIView, but when i rotate the device, UIView also rotates to Portrait. I don't want Portrait mode at all. I tried to handle this issue like below, but its not working for UIView. Then i saw this is possible in UIviewController, but not in UIView. But i must to have UIView for this camera launch oper开发者_JAVA百科ation.
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation==UIInterfaceOrientationLandscapeRight || interfaceOrientation==UIInterfaceOrientationLandscapeLeft);
}
Please suggest me how can i provide solution for orientation lock for my UIView?
Thank you!
Doubt over allowing from APPLE but If you want the UIImagePickerController to start(and stay) in Landscape orientation use following code.
//Initialize picker
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
//set Device to Landscape. This will give you a warning. I ignored it.
//warning: 'UIDevice' may not respond to '-setOrientation:'
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
//Set Notifications so that when user rotates phone, the orientation is reset to landscape.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
//Refer to the method didRotate:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification" object:nil];
//Set the picker source as the camera
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//Bring in the picker view
[self presentModalViewController:picker animated:YES];
The method didRotate:
- (void) didRotate:(NSNotification *)notification
{
//Maintain the camera in Landscape orientation
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
}
Credit for This code belongs to UIImagePickerController in Landscape
Try this:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
BOOL isLandscapeRight = (UIInterfaceOrientationLandscapeRight == interfaceOrientation);
return isLandscapeRight;
}
And Also Have to set Your Applications info.plist's interface orientation to Landscape (right home button)
There is a simple solution to this In the info.plist file edit the entry supported interface orientation ipad.
精彩评论