I have been searching for an answer to this, but cannot come up with anything. Apparently, iPhone SDK 3.0 made it possible that UIImagePickerController can be displayed in landscape mode - but I am not finding any method that will allow this. I would think that if the application is in landscape by default it would automatically adjust the image controller, but that is not working for me.
Thanks for any help!
No need to subclass; simply override the modalPresentationStyle property.
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.modalPresentationStyle = UIModalPresentationFormSheet;
[viewController presentViewController:picker animated:YES completion:NULL];
I haven't checked whether this is illegal, but it worked for me.
If you want the UIImagePickerController to start(and stay) in Landscape orientation 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];
}
If you just need to get rid of the warning try
#interface UIDevice ()
-(void)setOrientation:(UIDeviceOrientation)orientation;
#end
I've developed a UIImagePicker class in landscape mode. Works great for applications I've developed: hope it works for you too:
GitHub: https://github.com/imaginaryunit/iOSLandscapeThreadedImagePicker.git
I have an all-landscape app using UIImagePickerController too. Please be noted that if you call UIImagePickerController in Landscape mode, your app is possible to be rejected by Apple Review Team.
I devised a simple work around this issue which make use the shouldAutoRotate delegate. Apple approves this method for an all-landscape app.
See here for the details and downloadable full project source code.
Make a category of UINavigationController and add this method
- (BOOL)shouldAutorotate
{
return NO;
}
Subclass UIImagePickerController and override modalPresentationStyle as follows:
- (UIModalPresentationStyle)modalPresentationStyle
{
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
return UIModalPresentationFormSheet;
}
return [super modalPresentationStyle];
}
The image-picker is a form-sheet now and no longer in fullscreen-mode, but it looks good in landscape-mode.
This should be totally app-store-safe.
This works for the gallery, not for taking pictures.
I solved this problem as follows: after each change in orientation, I simple re-create picker. Try this. Differently is too crooked...
I am developing a class that tries its best to work in landscape mode.
Check it out on GitHub: RACameraController
Related
I'm working with an app which needs to support on both iOS6 and iOS7
The UIModalPresentationFormSheet (modalPresentationStyle) is not working for UIImagePickerController(For Camera) in iOS7 but it's working fine in iOS6.
What should I do?
According to Apple's documentation of UIImagePickerController, it should be displayed inside a popover. If you cannot accommodate this, you may have to roll your own, or use open source.
You can use below code for getting image from Camera or Gallery in ipad and iphone. As in ipad, you are not able to do presentmodalviewcontroller for getting image.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromRect:self.selectedImageView.bounds inView:self.selectedImageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popOver = popover;
} else {
[self presentViewController:picker animated:YES completion:nil];
}
I'm having a very strange problem. I want a video to appear in landscape mode, but I can't seem to make it work. Even if I can't make it always show Landscape, at least I want it to show ok, and I can't make that either!! Here is my code:
#import "SplashViewController.h"
#import "MainViewController.h"
#import "MediaPlayer/MediaPlayer.h"
#interface SplashViewController ()
#property (nonatomic, retain) NSTimer *timer;
#end
#implementation SplashViewController
#synthesize timer = _timer;
-(BOOL)shouldAutorotateToInterfaceOrientation:UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
- (id)init
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self = [self initWithNibName:#"SplashViewController_iPhone" bundle:nil];
} else {
self = [self initWithNibName:#"SplashViewController_iPad" bundle:nil];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSString *url = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"intro.mp4"];
playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewController moviePlayer]];
[playerViewController shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight];
[self.view addSubview:playerViewController.view];
//play movie
MPMoviePlayerController *player = [playerViewController moviePlayer];
player.scalingMode = MPMovieScalingModeAspectFill;
[player play];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[player.view removeFromSuperview];
[self loadMainView];
}
- (void)loadMainView
{
MainViewController *mainVC = [[MainViewController alloc] init];
[self.navigationController pushViewController:mainVC animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
And here comes the weirdness...
If I start the app with my iPad physically in Landscape Mode, the video shows like this (please not that the bar at the top is shorter than the widht! :O)
If I then rotate the iPad to Portrait, it looks like this:
But then, if I start the app with my iPad physically in Portrait Mode, the video shows like this:
And if I then rotate the iPad to Landscape, it looks like this:
Which is GREAT! This final image is what I would like the video to always look like.
Any ideas what I might be doing wrong???
Thanks!
EDIT 1
Ok, with #Tark answer I was able to fix the player display issue. Now it's showing fine no matter how I start the app. Thanks for that!! What is missing now is the always landscape mode.
I tried with the following methods:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation==UIInterfaceOrientationLandscapeRight)
return YES;
return NO;
}
I also tried inserting the row
Initial interface orientation = Landscape (right home button)
In the Info.plist
What I'm getting is that if I start the app in Landscape mode, if I rotate the iPad to Portrait, it stays in Landscape. GREAT!
But if I start the app in Portrait mode, the video shows in Portrait mode. Once I rotate it to Landscape, I can't rotate it back to Portrait, which is good, but I don't want it to start in Portrait!
EDIT 2
Ok, now this is even more weird. If I try it on an iPhone, it works great. No matter if I start the app in Landscape or Portrait, the video is shown always in Landscape.
But if I try it on an iPad, the problem in EDIT 1 arises... :S
Any ideas?
Thanks!
Have you tried setting the frame of the MPMoviePlayerViewControllers view when you add it as a subview?
...
playerViewController.view.frame = self.view.bounds;
[self.view addSubview:playerViewController.view];
...
To make the app only run in landscape mode, you should make sure that you have only selected the orientations you want in the app plist. In Xcode 4 there is a handy Supported Interface Orientations section in the target settings, make sure you only select landscape here. If you still have the issue, you have to make sure that you are disabling autorotation on all visible controllers in the view stack.
shouldAutorotateToInterfaceOrientation is deprecated as of iOS 6, Have you tried using supportedInterfaceOrientations?
If you are trying to support iOS 5 & 6 then I believe you need to use both:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
I haven't tested this so take it for what it's worth.
I am quite new to iOS and I am trying desperately to get orientation changes working in my app. After researching here's what I do:
In my app I have a NavigationController managing seven UIViewController subclasses.
In the projects summary tab I activated all 4 device orientations.
Each UIViewcontroller subclass has a xib file, all xib files have "autoresize subviews" activated.
The UIViewController subclasses all have:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight );
}
they also all have:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
and:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
implemented with an NSLog(...) statements (never printed, debugger also never entering these methods).
Also I was trying to use:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
BOOL getOrientationUpdates = [[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications];
NSLog(#"will receive orientation notifications: %#", getOrientationUpdates?#"YES":#"NO");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
with
-(void)orientationChanged: (NSNotification*) notification
{
NSLog(#"orientationChanged");
}
and
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self];
respectively.
when I do beginGeneratingDeviceOrientationNotifications etc. in the AppDelegate's - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method, orientationChanged: is called once on startup but never again however I rotate the device, when I do it in one of the UIViewController subclasses it is never called!
So far, all I want to achieve is getting orientation notifications to rotate an UIImageView and UIImage (without any layout changes in the different orientations).
UIDeviceOrientation o = [UIDevice currentDevice].orientation;
always returns UIDeviceOrientationPortrait
It might be that I missed something in the docs or on stackoverflow, but I obviously cannot figure out what I need to do/add to get it working in my setup. also I am quite new to stackoverflow, so I hope my post is not violating any platform conventions.
Any help/hints are greatly appreciated. Thank you so much!
EDIT:
getOrientationUpdates is always YES, which appears strange to me as the notification callback selector is never called when I rotate it!
EDIT: in my AppDelegate's didFinishLaunchingWithOptions I am doing:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.regScreenController = [[RegistrationScreenController alloc] initWithNibName:#"RegistrationScreenController" bundle:nil];
navCtrl = [[UINavigationController alloc]initWithRootViewController:self.regScreenController];
[navCtrl setNavigationBarHidden:YES];
self.window.rootViewController = self.regScreenController;
[self.window addSubview:navCtrl.view];
[self.window makeKeyAndVisible];
return YES;
See if you are setting the self.window.rootViewController in your AppDelegate's didFinishLaunchingWithOptions: method, because if you're adding only subviews to the window the orientation change notification does not fire.
I upgraded to xCode 4.2 and it's new Storyboards feature. However, could not find a way to support both portrait and landscape.
Of course, I did it programmatically, with 2 views, one for portrait and one for landscape, like in old days, and:
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
self.view = self.landscapeView;
}
else
{
self.view = self.portraitView;
}
But I was looking for a way to do this automatically somehow. I mean, it's xCode 4.2 now, I expected more from it. Thanks all.
==================================
TEMPORARY SOLUTION:
I will present here a temporary solution. I say it's temporary, because I am still waiting for Apple guys to do something really intelligent about this.
I created another .storyboard file, called "MainStoryboard_iPhone_Landscape", and implemented the landscape view controllers there. Actually, it's exactly like normal(portrait) .storyboard, but all screens are in landscape mode.
So I will extract the ViewController from landscape storyboard, and when rotation occurs, just change self.view with the new viewController's view.
1.Generate Notifications when orientation changes:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
2.Look for notifications:
[[NSNotificationCenter defaultCenter] addObserverForName:UIDeviceOrientationDidChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
// We must add a delay here, otherwise we'll swap in the new view
// too quickly and we'll get an animation glitch
[self performSelector:#selector(updateLandscapeView) withObject:nil afterDelay:0];
}];
3.Implement updateLandscapeView
- (void)updateLandscapeView {
//> isShowingLandscapeView is declared in AppDelegate, so you won't need to declare it in each ViewController
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !appDelegate().isShowingLandscapeView)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone_Landscape" bundle:[NSBundle mainBundle]];
MDBLogin *loginVC_landscape = [storyboard instantiateViewControllerWithIdentifier:#"MDBLogin"];
appDelegate().isShowingLandscapeView = YES;
[UIView transitionWithView:loginVC_landscape.view duration:0 options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationCurveEaseIn animations:^{
//> Setup self.view to be the landscape view
self.view = loginVC_landscape.view;
} completion:NULL];
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) && appDelegate().isShowingLandscapeView)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
MDBLogin *loginVC = [storyboard instantiateViewControllerWithIdentifier:#"MDBLogin"];
appDelegate().isShowingLandscapeView = NO;
[UIView transitionWithView:loginVC.view duration:0 options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationCurveEaseIn animations:^{
//> Setup self.view to be now the previous portrait view
self.view = loginVC.view;
} completion:NULL];
}}
Good luck to everybody.
P.S: I will accept Ad Taylor's answer, because, after much time waiting and searching for a solution, I finished up implementing something inspired from his answer. Thanks Taylor.
This is an old question but I read this earlier in the day and then had to spend a fair amount of time work out a better solution. I came up with this solution from hacking up the Apple Alternate View example. Basically it is serving up a modal view for the landscape view.
#pragma mark Rotation view control
- (void)orientationChanged:(NSNotification *)notification
{
// We must add a delay here, otherwise we'll swap in the new view
// too quickly and we'll get an animation glitch
[self performSelector:#selector(updateLandscapeView) withObject:nil afterDelay:0];
}
- (void)updateLandscapeView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !self.isShowingLandscapeView)
{
[self performSegueWithIdentifier: #"toLandscape" sender: self];
self.isShowingLandscapeView = YES;
}
else if (deviceOrientation == UIDeviceOrientationPortrait && self.isShowingLandscapeView)
{
[self dismissModalViewControllerAnimated:YES];
self.isShowingLandscapeView = NO;
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
You shouldn't generally be thinking of separate views for different orientations unless they are widely different (which, arguably, they shouldn't be). Instead, you should rely on autoresizing masks to lay out as much of your view's content based on basic restraints when the superview's frame changes. This will allow subviews to respond appropriately to a change in their superview's frame, often as a result of an interface orientation change.
To answer your question more directly, no, there is no way for Xcode to assume or be told which views you want to use for a particular interface orientation as this was never the intent of UIKit's view architecture.
Here is more information about autoresizing masks: Handling Layout Changes Automatically Using Autoresizing Rules.
In XCode v4.2.1 when using StoryBoards you can only change the orientation of the View Controller, and not the View itself, so if you have inserted another view there you wouldn't be able to change it's orientation, even if you could see the View properly.
So the previous way of having two Views would not appear to work when using StoryBoards (when using NIB's where the View Orientation is changeable for separate Views).
Can anyone confirm that [[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait]; will get your app rejected by Apple.
Is there a good replacement available?
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientation] is a private API, you can find that is signature is only in read-only (see here).
If you want set the device orientation to Portrait or other mode you should override the shouldAutorotateToInterfaceOrientation:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIDeviceOrientationPortrait);
}
Yes you'll get rejected.
You can change the frame and apply a transformation to the key window, and change the status bar orientation (which is public) to simulate changing the orientation.
You can add UIInterfaceOrientation in your app plist
You can try this code. It works perfectly on my apps
UIApplication* application = [UIApplication sharedApplication];
if (application.statusBarOrientation != UIInterfaceOrientationPortrait)
{
AppDelegate* app = [[UIApplication sharedApplication] delegate];
UIViewController *c = [[UIViewController alloc]init];
[app.viewController presentModalViewController:c animated:NO];
[app.viewController dismissModalViewControllerAnimated:NO];
[c release];
}
where app.viewController is the rootViewController of the app