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;
}
Related
Hello I am quite new to iPhone development (working with iOS 7). Basically I want my application to respond to screen rotations (i.e. changing the screen orientation) My project uses both UITabBarController and UINavigationController. However when I rotate the device it won't call the "shouldAutorotate" function in a ViewController called LoginView.m.
Therefore I have followed the answer in here up to the point where it subclasses both UITabBarController and UINavigationController. Can someone explain to me how to add (i.e reference it)the orientation behaviour within the LoginView.m class or to the entire project.
Your help is greatly appreciated.
Thanks
In your subclassed UItabbarcontroller
- (BOOL)shouldAutorotate {
UINavigationController *navView = (UINavigationController *)[self selectedViewController];
//Get the selected current tab viewcontroller. I guess you are having a Navigationcontroller
UIViewController *vc = [navView.viewControllers objectAtIndex:0];
//Fetch root viewcontroller of your navigationcontroller
return [self checkOrientationForViewController:vc];
}
-(BOOL) checkForViewsForViewController : (UIViewController *)vc{
if([vc isKindOfClass:[FirstViewController class]]){
FirstViewController *vc1 = (FirstViewController *)vc;
return [vc1 shouldAutorotate];
}
if([vc isKindOfClass:[SecondViewController class]]){
SecondViewController *vc2 = (SecondViewController *)vc;
return [vc2 shouldAutorotate];
}
if([vc isKindOfClass:[ThirdViewController class]]){
ThirdViewController *vc3 = (ThirdViewController *)vc;
return [vc3 shouldAutorotate];
}
return YES;
}
In respective viewcontrollers implement the shouldAutorotate method
Instead of using Delegation you can also use
- (NSUInteger)supportedInterfaceOrientations
UINavigationController and UITabBarController ask their child view controllers which interface orientations they support. So you can implement "- (NSUInteger)supportedInterfaceOrientations" in your LoginView.m and return the appropriate interface orientations.
You also need to edit your Info.plist and add the supported interface orientations their. You can do this with Xcode by opening the project view. Also have a look at Apples documentation on supporting interface orientations
Instead of subclassing, is worth to know that there are also delegate methods, in UINavigationcontroller and UITabbarController that make you handle rotations at runtime:
- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController
Ref doc.
- (NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController
Ref doc.
Evenf if Apple has removed the advice to do not subclass Nav and Tabbar controller, using delegation will be the most reliable solution over time
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];
I started with a GLSprite sample app (source code), then added a UIViewController by adding
UIViewController *vc = [[UIViewController alloc] init];
vc.view = glView;
self.window.rootViewController = vc;
[window makeKeyAndVisible];
to the end of GLSpriteAppDelegate::applicationDidFinishLaunching. The controller seems to be working as now I can pop up gamecenter windows, but it has messed up my screen orientation. The app is fine in portrait, but in all other rotations it incorrectly has white bars on the side, like one of the views is rotated 90 degrees.
I was doing [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]; in EAGLView::initWithCoder, I then tried moving it to the end of applicationDidFinishLaunching but it had the same behavior.
Can anyone help? What can I do to fix or debug this? Thank you!
The issue was the new UIViewController was rotating, when my game expects an always portrait screen. To fix I created a custom viewcontroller and implemented
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return 0;
}
I have now been stuck on this problem for more then 2 weeks! In my project, I have 1 single ViewController(slide) I want to enable both landscape and portrait in. The rest of all controllers/views(slides) I want to enable portrait-mode only.
The tricky part is, the "ViewController" I am referring to is connected to both NavigationControllers and TabBarControllers. See the scheme below where the ViewController I want to enable both landscape/portrait is named: ReferredViewController.
TabBarController ----> NavigationController ----> FristViewController --(push event)--> ReferredViewController
So far I have tried to make a CATEGORY for both NavigationControllers and TabBarControllers. But since my NavigationControllers and TabBarControllers are placed at the very start of the project this will set the rules for the whole project. My ReferredViewController is placed at the end or in the middle of the projects "storyboard". I have tried to set the rules by code aswell for the single ReferredViewController without any success.
My best shot is to change the event between FirstViewController and ReferredViewController from "push" to "modal". ReferredViewController can then rotate both portrait/landscape and the rest of the project is locked in portrait. BUT, as you may know all navigations (NavigationBar) will be lost and the user will become stuck at that single slide.
So I am trying to enable the NavigationBar with the following code example in the ReferredViewController.m file:
ShowTaskViewController *detailViewController = [[ShowTaskViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentModalViewController:navController animated:YES completion:nil];
[navController release];
[detailViewController release];
But ofc nothing happens and I am back to square one again :O. FML!
In this line:
[self.navigationController presentModalViewController:navController
animated:YES
completion:nil];
you are conflating two UIViewController instance methods:
- (void)presentViewController:(UIViewController *)viewControllerToPresent
animated:(BOOL)flag
completion:(void (^)(void))completion
- (void)presentModalViewController:(UIViewController *)modalViewController
animated:(BOOL)animated
The first of these is now the standard, the second method was deprecated in ios6.
Also the presenting view controller should be self (the ReferredViewController), not self's navigationController.
Your presented view controller can dismiss itself thus
[[self presentingViewController] dismissViewControllerAnimated:YES
completion:(void (^)(void))completion];
But take a look at fibnochi's answer, it may be a better way for you to achieve your result.
you have to over ride UITaBarController because it is you base view controller. I have done this for my navigation controller. Tell me if this helped.
#interface UINavigationController (Autorotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
- (BOOL) shouldAutorotate;
- (NSUInteger) supportedInterfaceOrientations;
#end
#implementation UINavigationController (Autorotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
if ([self.visibleViewController isKindOfClass:[MWPhotoBrowser class]] || [self.visibleViewController isKindOfClass:[ZoomPictureViewController class]]) {
return YES;
}
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL) shouldAutorotate{
return YES;
}
-(NSUInteger) supportedInterfaceOrientations{
if ([self.visibleViewController isKindOfClass:[MWPhotoBrowser class]] || [self.visibleViewController isKindOfClass:[ZoomPictureViewController class]]) {
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
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.