Disable UITableViewcontroller from rotating in Landscape (keep it Portrait orientation) - ios

I'm trying to keep UITableViewcontroller in Portrait orientation. Hence, I don't want to rotate to Landscape mode. I added below method. But it didn't help, notice I'm using iOS 8:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if(interfaceOrientation== UIInterfaceOrientationPortrait)
{
return YES;
}else
{
return NO;
}
}
Notice, I'm calling the UITableView through UINavigationController
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:svc];
// configure the new view controller explicitly here.
[self presentViewController:navigationController animated:YES completion: nil];

You can set the enabled orientation interface in the Info.plist or, if you want only the Table in this way, you have to modify manually the supportInterfaceOrientation in the navigationController when you are presenting the view. The 'child' will assume the values from the navigationController. Then, when you dismiss the table, you have to reset the supportInterfaceOrientation manually.

Related

Landscape mode in presented view controller

I have an app in which only portrait mode is allowed. I am using a subclassed qlPreviewController for showing preview of an image. All orientations are allowed in this. But when I dismiss the preview controller in landscape mode , my UIView Controller's ui collapses. I i use following code in UIViewController's viewWillAppear method, My ui becomes correct.
if i use
-(void)viewWillAppear
{
UIViewController *c = [[UIViewController alloc]init];
[c.view setBackgroundColor:[UIColor clearColor]];
[self.navigationController presentViewController:c animated:NO completion:nil];
[self performSelector:#selector(delay) withObject:self afterDelay:1.0 ];
}
-(void)delay
{
[self.navigationController dismissViewControllerAnimated:NO completion:nil];
}
Can any one suggest the reason for this and a better method to do it
In order to restrict the app to only use portrait mode, I created a BaseViewController class as subclass of UIViewController and allowed only portrait mode in it. I created all other view controllers in the app as a subclass of this BaseViewController. Code in the BaseViewController is :-
-(BOOL)shouldAutorotate
{
return FALSE;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}

shouldAutorotate in subclassed UINavigationController is returning NO but rotation still occurs

I have a view controller which I want to lock down in portrait mode. By creating a custom navigation controller I have been able to force portrait mode when the device is in landscape, and that works initially when the app starts up.
But when I rotate the device to portrait mode and then back to landscape, the orientation changes again.
I would assume that shouldAutorotate returning NO should prevent this, but it doesn't for some reason. I verified that function is being called every time I physically rotate the device.
Anyone know why iOS is ignoring my shouldAutotate of NO?
I've read many posts on this and my code seems to be what many people are suggesting, but doesn't work.
#interface MyUINavigationController : UINavigationController
#end
#implementation MyUINavigationController
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait ;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotate
{
NSLog(#"shouldAutorotate called and returning NO");
return NO;
}
#end
// This is how I am using my custom Navigation Controller:
UINavigationController* mynav = [[MyUINavigationController alloc] initWithRootViewController:customViewController];
visibleVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];
[mynav setModalPresentationStyle:UIModalPresentationFullScreen];
[mynav setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[visibleVC presentViewController:mynav animated:YES completion:nil];

UITableViewController subView rotation

I'm new to stackoverflow and to objective-C programming. I have searched for the issue described below, but I'm not able to find a working solution.
My application is a simple offline browsing app, with navigation structure.
In the appDelegate I load the RootViewController (UITableViewController) in one of the following ways:
Solution A
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
Solution B
RootViewController* rootviewcontroller = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:rootviewcontroller];
The rootViewController simply push some views, i.e.
TipAndTrickViewController *mTipAndTrick = [[TipAndTrickViewController alloc]initWithNibName:#"TipAndTrickViewController" bundle:nil];
[self.navigationController pushViewController:mTipAndTrick animated:YES];
In the deeper view I present a detail modalView (UIViewController).
What I want is to enable autorotate only in this last view. The portait orientation is the desired for all the previoues wiews. The last view implements in the right way:
shouldAutorotateToInterfaceOrientation:interfaceOrientation
shouldAutorotate
willRotateToInterfaceOrientation:toInterfaceOrientation
duration:duration
willAnimateRotationToInterfaceOrientation:interfaceOrientation
duration:duration
Overriding
shouldAutorotate
shouldAutorotateToInterfaceOrientation:interfaceOrientation
making them returning NO/YES in the rootViewController and setting the allowed orientation in the desired way, using
supportedInterfaceOrientations
(both in rootViewCOntroller and in the last view), I get those results:
if I use Solution A all the views don't rotate.
if I use Solution B all the views always rotate.
What I'm doing in the wrong way?
Thank you in advance for your help
Add these to your viewController and let me know if it works or not .
// iOS5 Rotation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
// iOS6 Rotation
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotate
{
return YES;
}

Orientation Landscape -> Portrait isn't working

I have another orientation problem. But this one is very tricky.
My RootViewController is a normal NavigationController.
self.window.rootViewController = _naviController;
which has another ViewController inside, lets call it VC1.
VC1 has some buttons and labels. Its like an overview with folders.
If I press a button I come to the next ViewController with 3 ViewController (Page) and another bunch of buttons (like inside a folder looking at the pictures/thumbnails inside):
Archiv *archiv = [[Archiv alloc] init];
[self.navigationController pushViewController:archiv animated:YES];
[archiv release];
in loadView:
firstPage = [[Page alloc] initViewWithFrame:CGRectMake(0, 0, 768, 960)];
[firstPage setRootViewController:self];
secondPage = [[Page alloc] initViewWithFrame:CGRectMake(0, -960, 768, 960)];
[secondPage setRootViewController:self];
thirdPage = [[Page alloc] initViewWithFrame:CGRectMake(0, 960, 768, 960)];
[thirdPage setRootViewController:self];
If I now click again on a button the active Page push my third ViewController (image with resizing, dragging...):
Picture *pic = [[Picture alloc] initWithPicURLString:urlString];
[rootViewController.navigationController pushViewController:pic animated:YES];
[pic release];
With the BackButton of the NavigationController I can always come back to the previous view.
Some more informations:
Every ViewController supports all orientations
Every ViewController implements - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation with return YES;
Every ViewControler calls the [super init] in their init-methode
I already read Apple's Q&A: Why won't my UIViewController rotate with the device
Now the tricky problem:
If I switch from 2nd VC to the 3rd VC, change the orientation there from portrait to landscape and press the BackButton everything is working (shouldAutorotateToInterfaceOrientation is calling, frame size and origins changing ...).
BUT if I do it the other way around, I am in landscape mode, switch from 2nd VC to 3rd VC, rotate to portrait and come back to 2nd VC with BackButton, the status- and controllerBar are at the top but the shouldAutorotateToInterfaceOrientation wasn't called.
Please help me. $h#rky
Try this, it works for me:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self shouldAutorotateToInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation] ];
}
Today I got the idea that solved the problem without knowing the cause.
In my third VC I just created a pointer to the 2nd View and called the shouldAutorotateToInterfaceOrientation myself.
But the point is still the same: Why isn't shouldAutorotateToInterfaceOrientation not calling in the described situation?
Kind regards. $h#rky
shouldAutorotateToInterfaceOrientation only called when user rotate, so when you from landscape to portrait or otherwise then view controller still landscape, so this solve problem, you have to hack code, it's mean when you push to view controller from landscape to portrait presentViewController example:
ListCurrentViewController *list = [self.storyboard
instantiateViewControllerWithIdentifier:#"ListCurrentViewController"];
[self.navigationController presentViewController:list animated:NO completion:Nil];
[list dismissViewControllerAnimated:NO completion:Nil];
[self.navigationController pushViewController:list animated:YES];
in ListViewController function called:
(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // iOS 6 autorotation fix { return UIInterfaceOrientationPortrait; }
and you have to create category for UINavigationController
(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [self.visibleViewController preferredInterfaceOrientationForPresentation];
}
I hope this solve will help you.

Modal View using UIModalPresentationFormSheet appears offscreen

I have a UIViewController that implements
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Then, I try to popup a modal on top of that view:
ModalViewController *modalViewController = [[ModalViewController alloc] init];
modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
modalViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[mainViewController presentModalViewController:modalViewController animated:YES];
If I launch the modal while the ipad is in portrait, it works fine. But when I'm holding it in landscape and try to launch the modal, the modal appears half offscreen to the upper right of the ipad. Any ideas?
In ModalViewController, implement shouldAutorotateToInterfaceOrientation the same way that it is in mainViewController (both need to agree on the orientations they support).

Resources