How to detect the orientation changing ipad - ios

Im creating an iPad application that should be supported both landscape and portrait. I have set root view controller in AppDelegate like this. This view is my Splash screen view.
Inside
`
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft)
{
//Do what you want in Landscape Left
viewController1=[[SplashLandscapeViewController alloc] initWithNibName:#"SplashLandscapeViewController" bundle:nil];
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
//Do what you want in Landscape Right
viewController1=[[SplashLandscapeViewController alloc] initWithNibName:#"SplashLandscapeViewController" bundle:nil];
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait)
{
//Do what you want in Portrait
viewController1=[[SplashLandscapeViewController alloc] initWithNibName:#"SplashViewController" bundle:nil];
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
//Do what you want in Portrait Upside Down
viewController1=[[SplashLandscapeViewController alloc] initWithNibName:#"SplashViewController" bundle:nil];
}
self.window.rootViewController = viewController1;
[self.window makeKeyAndVisible];
return YES;
}
And this is working fine. And this viewcontroller1 is dissmiss after 4,5 seconds and load another viewcontroller. My problem is while that viewcontroller1 showing on the screen, if user turn the ipad how can I recogniz it and load my landscape .xib after removing the current portrait xib.
Please help me.
Thank you

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(changeOrientation) name:UIDeviceOrientationDidChangeNotification object:nil];
add this method into the app view controller for getting notification.
-(void)changeOrientation {
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
[[NSBundle mainBundle] loadNibNamed:#"Portrait" owner:self options:nil];
}
else {
[[NSBundle mainBundle] loadNibNamed:#"Landscape" owner:self options:nil];
}
}

Related

MPMoviePlayerViewController in Landscape when dismissed Portrait

I have got a TableViewController which is only for PortraitMode. Now I want to play a video when a cell is touched.
In my App Delegate I am working with the method:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]]) {
NSLog(#"ja1");
return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
if ([[self.window.rootViewController presentedViewController] isKindOfClass:[UINavigationController class]]) {
NSLog(#"ja2");
// look for it inside UINavigationController
UINavigationController *nc = (UINavigationController *)[self.window.rootViewController presentedViewController];
// is at the top?
if ([nc.topViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
NSLog(#"ja3");
return UIInterfaceOrientationMaskAllButUpsideDown;
// or it's presented from the top?
} else if ([[nc.topViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]]) {
NSLog(#"ja4");
return UIInterfaceOrientationMaskAllButUpsideDown;
}
}
}
return UIInterfaceOrientationMaskPortrait;
}
to make it possible to turn to landscape while watching the video.
I show an instance of MPMoviePlayerViewController from my TableView
- (void)playVideo {
NSURL *movieURL = [NSURL URLWithString:MYURL];
MPMoviePlayerViewController *c = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
// Remove the movie player view controller from the "playback did finish" notification observers
[[NSNotificationCenter defaultCenter] removeObserver:c
name:MPMoviePlayerPlaybackDidFinishNotification
object:c.moviePlayer];
// Register this class as an observer instead
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:c.moviePlayer];
// Set the modal transition style of your choice
c.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
// Present the movie player view controller
[self presentViewController:c animated:YES completion:^{}];
// Start playback
[c.moviePlayer prepareToPlay];
[c.moviePlayer play];
}
Now I have the problem, that when I dismiss the MPMoviePlayerViewController in landscape mode I want to rotate it back to Portrait mode. But my MasterViewController (=TableView) is still in landscape mode.
Can you help me out!
Implement following orientation methods in your MasterViewController. which will make your MasterViewController to stay portrait.
- (BOOL)shouldAutorotate{
return false;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
// Returns interface orientation masks.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}

UIDeviceOrientationPortrait , ios 6 & 7 , InterfaceOrientation

i have this code i want to go to another uiview but i want to have showing this view (second view) in portrait mode only and the notification in first view doesn't work in second view ;
is there any way to change the for this
[[NSNotificationCenter defaultCenter] removeObserver:self];
view2 *viewController = [[[view2 alloc] init] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
viewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:navController animated:NO];
i want exactly one function same this code in ios >6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NO;
}
Try subclassing the UINavigationController and override the following methods :
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}

FPPopover not showing

I'm implementing FPPopover into my application, it was working alright until I added some code to the app delegate to manage and display the correct storyboard depending on the device throughout the app. The code I added is as follows...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
UIStoryboard *storyboard = [self getStoryboard];
UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:#"Init"];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = initViewController;
[self.window makeKeyAndVisible];
sleep(3);
return YES;}
-(UIStoryboard*) getStoryboard {
UIStoryboard *storyBoard = nil;
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPad" bundle:nil];
}else{
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone){
// The iOS device = iPhone or iPod Touch
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if (iOSDeviceScreenSize.height == 480){
// iPhone 3/4x
storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
}else if (iOSDeviceScreenSize.height == 568){
// iPhone 5 etc
storyBoard = [UIStoryboard storyboardWithName:#"Storyboard_iPhone_5" bundle:nil];
}
}
}
return storyBoard;}
If I delete all this code from my App delegate the popover works perfectly... But if I add it it doesn't work... I have no idea what is going on.
Can anyone help?
You don't need of this code..go in the project's general tab and set your storyboard for iPhone and iPad here:
In the storyboard you can select the inital view controller..
So you can delete all your code for storyboard and initial viewController
This is the code that i use in my appDelegate :) :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}

Switching views upon rotation not working properly

I've been trying several approaches to get the rotation working as expected but so far I've been facing some problems.
Now I would like to have one class with one xib file containing the 2 orientation views. One for portrait, one for landscape.
I am trying this in my main ViewController (the root controller set in the app delegate. But somehow it's failing : when I rotate, the view is switched but the outlets are not rotated. For instance a simple label from the landscape view will be shown as if the device was in portrait mode.
Here's bits of code :
In my app delegate
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
} else {
//self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil withOrientation:UIInterfaceOrientationPortrait];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.viewController;
In Viewcontroller.m
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
[self performSelector:#selector(updateLandscapeView2) withObject:nil afterDelay:0];
}
- (void)updateLandscapeView2
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation))
{
self.view = self.landscapeView;
[[NSNotificationCenter defaultCenter] postNotificationName:#"landscape" object:nil];
}
else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
{
self.view = self.portraitView;
[[NSNotificationCenter defaultCenter] postNotificationName:#"portrait" object:nil];
}
}
I intend to forward the orientation change to the subviews of Viewcontroller by using local notifications.
Anyway, what am I missing here ? I don't understand why this is failing.
What i have found is that when you call
[UIDevice currentDevice].orientation
inside the context of a
[viewController performSelector .....]
It didn't work.
Some body with a solution for these?

Switch View in XIB based on orientation

I have two views inside my XIB for each orientation. They are similar but different so I cannot rely on the autoresizing. I want to switch these views based on the orientation. When I use the method didRotateFromInterfaceOrientation then when it rotates from Portrait it will show the landscape view, this is a problem when it is rotating to PortraitUpsideDown.
And if I modify the code for willRotateToInterfaceOrientation then in LandscapeView the PortraitView is hidden from the user but when the user clicks the buttons they do not show gray out to show that they were clicked and it app responds as if the user is clicking the buttons that are in the portrait view.
Am I going about this problem in the right manner? What do I need to do to fix these errors. I would prefer to use the method willRotateToInterfaceOrientation.
Thanks,
CODE:
- (void)viewDidLoad {
...
for (UIView *v in self.view.subviews) {
v.autoresizingMask = UIViewAutoresizingNone;
}
...
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if(fromInterfaceOrientation == UIInterfaceOrientationPortrait
|| fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
//Add landscape view
if(![landscapeView superview]){
for (UIView *v in landscapeView.subviews) {
v.autoresizingMask = UIViewAutoresizingNone;
}
//No effect after testing
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.33];
[self.view addSubview:landscapeView];
[UIView commitAnimations];
}
}else {
if([landscapeView superview]){
[landscapeView removeFromSuperview];
}
}
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
//Add landscape view
if(![landscapeView superview]){
for (UIView *v in landscapeView.subviews) {
v.autoresizingMask = UIViewAutoresizingNone;
}
//No effect after testing
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.33];
[self.view addSubview:landscapeView];
[UIView commitAnimations];
}
}else {
if([landscapeView superview]){
[landscapeView removeFromSuperview];
}
}
}
In Your View Did Load
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(willRotateToInterfaceOrientation:)
name:#"UIDeviceOrientationDidChangeNotification" object:nil];
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) {
viewControllerName *newView = [[viewControllerName alloc] initWithNibName:nil bundle:nil];
newView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:newView animated:YES];
[newView release];
}
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) {
viewControllerName *newView = [[viewControllerName alloc] initWithNibName:nil bundle:nil];
newView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:newView animated:YES];
[newView release];
}
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) {
viewControllerName *newView = [[viewControllerName alloc] initWithNibName:nil bundle:nil];
newView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:newView animated:YES];
[newView release];
}
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) {
viewControllerName *newView = [[viewControllerName alloc] initWithNibName:nil bundle:nil];
newView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:newView animated:YES];
[newView release];
}
}

Resources