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 operation.
-(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.
Related
I have been working an iOS 7 app to make it compatible for ios 8 (beta 5). In this application, UIViewController (vc1) presents another UIViewController (vc2). vc1 supports both Portrait and Landscape orientations; vc2 supports only Portrait orientation. When vc2 is presented, it asks vc1: shouldAutorotateToInterfaceOrientation: and this returns YES.
In iOS8 (Beta 5) willRotateToInterfaceOrientation: and didRotateFromInterfaceOrientation: are not getting called as well as the new iOS 8 API method viewWillTransitionToSize. But, this works fine in iOS7.
I know willAnimateRotationToInterfaceOrientation and didRotateFromInterfaceOrientation are deprecated in iOS 8, but even iOS 8 delegate methods are not getting called. Every time when launched vc2 from vc1 always screens loads in portrait mode only even though I mentioned supported interface orientation as landscape left.
Any ideas... is it a bug in iOS8?
Well, I didn't figure out your problem best but as soon I have a bunch of lines working fine with rotation in iOS 8.1 I will present them to you. They are just taken and a little bit of edited from the Apple API Reference.
Simply I put this in every VC and i just edit the code when needed. For example I have an app that have initial view controller in portrait and then VC changes ( segue is done ) to a LandscapeVC with different features.
This is the portrait view methods leading to a rotation in LandscapeView.
bool isShowingLandscapeView = false;
- (void)awakeFromNib
{
isShowingLandscapeView = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
!isShowingLandscapeView)
{
isShowingLandscapeView = YES;
[self performSegueWithIdentifier:#"toLandscape" sender:self];
}
I hope I made it simple for understanding. Don't hesitate to improve my answer, we all learn in this life !
my project support portrait and landscape orientations on the device orientation on the general information.
also, most of the viewcontrollers has a superclass named paren view controller in which i implemented methods to show the view only in portrait this method is -shouldautorotate returning NO. This specific controller containing the webview as a one of many views should be in portrait to, so i implemented that method too but i need the video to be in landscape also at the moment it is playing when the done button on the player is tapped and the video disappear the main view has to be on portrait, in other words i need the video to be in landscape and in portrait orientation.
when i return YES from the shouldautorotate method the view containing the webview and the video can rotate which is what i don't want to happen.
Any help to solve this?
You can handle the rotation manually by adding observer.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationDidChange:)
name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationDidChange:(NSNotification*)notification
{
/// handle rotation for your views
}
I am developed an application which supports both Portrait and Landscape orientation with the GPOrientationKit. I followed following link to do all the pages of the app to support both the orientations.
http://logisian.blogspot.in/
I did that app with XIB or NIB file. Now, I am currently working on an app with the storyboard. I don't know how to do the app which support both the orientations with the storyboard. GPOrientationKit worked well for XIB files. But I am struggling with storyboard.I need the functionality like GPOrientationKit with the storyboard. Please help me to do that.
Thanks in Advance.
Just add two subview to your mainView of your controller such as portraitView and landscape View, and toggle between those to as and when your orientation changes..I had something like this
In your viewWillAppear method add this
-(void)viewDidAppear:(BOOL)animated{
if(UIDeviceOrientationIsPortrait(self.interfaceOrientation))
{
//Keep LAndscape View Hidden
self.portraitVIew.frame=self.view.frame;
[self.view addSubview:self.portraitVIew];
}else{
//Keep portrait View Hidden
self.landscapeView.frame=self.view.frame;
[self.view addSubview:self.landscapeView];
}
self.view.autoresizesSubviews = YES;
}
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(deviceOrientationDidChangeNotification:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
and then implement the method deviceOrientationDidChangeNotification such as
- (void)deviceOrientationDidChangeNotification:(NSNotification*)note
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(UIDeviceOrientationIsLandscape(self.interfaceOrientation))
{
//Keep portrait View Hidden
NSLog(#"YUP THIS IS LANDSCAPE");
self.landscapeView.hidden=NO;
self.landscapeView.frame=self.view.frame;
[self.portraitVIew removeFromSuperview];
///self.landscapeView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:self.landscapeView];
}else {
//Keep LAndscape View Hidden
self.landscapeView.hidden=YES;
self.portraitVIew.frame=self.view.frame;
NSLog(#"Portrait");
[self.view addSubview:self.portraitVIew];
}
}
you click to project>select target > General you set the device Orientation.
I'm creating an iOS application and i've encountered the following problem:
The project is set to be a portrait mode application.
I have a UIWebview, which loads a webpage with a youtube video in it. When the video is clicked it starts in the youtube player. When the video changes to landscape mode and 'done' is clicked it returns to the UIWebview showing it in landscape.
I do receive a NSNotification when the video is finished but I'm not able to force portrait mode.
This should not be possible, how can I allow youtube to be in landscape but force the app to stay in portrait mode? Or is there another workaround?
EDIT:
After contact with Apple it turned out to be a bug and is reported.
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
is private API . I would not recommend using it.
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *view = [window.subviews objectAtIndex:0];
[view removeFromSuperview];
[window insertSubview:view atIndex:0];
is much better way i think. It forces the app to adjust its orientation.
I think you can use NSNotification as you said and in called method use this line of code for now to force view to portrait
- (void)YourMethodCalledByNSNotification
{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}
}
Hope that Helps
So I have a UISplitViewController that is the root view of my program. I have set the initial orientation to be LandscapeLeft and have all 4 orientations supported in my plist.
If I launch the app in portrait - the view that is shown is my root view not the detail view which obviously is not what I want. As soons as I rotate the device, everything works from there.
I have scoured this site and others but have found no fixes. The closest I have come is the adding the following to my app delegate:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
if (UIDeviceOrientationIsValidInterfaceOrientation([UIDevice currentDevice].orientation)) {
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
else
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
}
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
So this essentially forces it to draw correctly, which works great except for when the app launches with the device laying flat. Then I dont know the portrait vs landscape (and I know of no way to find it). So I basically then cant accurately say what to do.
The larger thing is this above code is a HACK, and there should be a better way, but for the life of me I cant find it.
Any ideas?
Thanks for your code on fixing this for portrait and landscape. I found a quick fix for the issue when its flat on the desk or upside down(not sure who would start the app upside down, but the same issue happens in this case):
if (UIDeviceOrientationIsValidInterfaceOrientation([UIDevice currentDevice].orientation)) {
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
else if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
}
else {
if (([UIApplication sharedApplication].statusBarOrientation == 1) || ([UIApplication sharedApplication].statusBarOrientation == 0))
{
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
}
else {
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
}
NSLog(#"orientation: %d", [UIApplication sharedApplication].statusBarOrientation);
}
In my App, this works because I just wanted to force them into portrait to start and if they move to landscape after that, it rotates and works fine because I support all orientations.
I don't think this will work in all cases, but this might help you get on the right track. You just need to play with the statusBarOrientation value and manually set it based on what you find works if the InterfaceOrientation is not valid(which it isn't if the device is flat on a table or upside down).
Hope that helps...
Edit: Also, I have a need in my App for knowing orientation while they are picking new items in the SplitViewController, so it was being wonky when the device was flat or upside down. And the way I detected if it was flat or upside down was to first see if the orientation was valid like you did:
if (UIDeviceOrientationIsValidInterfaceOrientation([UIDevice currentDevice].orientation))
But then if its not, I check to see if self.popoverController is nil. If its not, then its in portrait and if it is, then its landscape (based on how the splitViewController works by setting that to nil or not):
if (self.popoverController)
{
NSLog(#"in portrait");
}
else {
NSLog(#"in landscape");
}
This wouldn't work in the App Delegate while the App is launching though because this detailViewController isn't being used at that point. Just thought I'd let you know if it helps out any.
Your RootViewController is showing in place of your detailViewCOntroller?
it seems you're doing something bad... (maybe inverted viewControllers order in SplitViewController.viewControllers ?)
You need to make sure to add your UISplitViewController's view to the UIWindow INSIDE the application:application didFinishLaunchingWithOptions: method of your app delegate (not later), or the split view won't rotate correctly initially.