0

In many situation need to rotate the controller and is not working. Right now I have the inverse of the problem: it is rotating, and I want to disable.

In that ViewController I have this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

but it is auto-rotating, because not this UIViewController is asked, but his parent in UI tree. Maybe that is the problem. The root controller must return Yes for all cases, because there are a few other UIViewControllers on the stack, which has / must have Portait / Landscape support.

I can't / don't want to touch other parts, because ... there are several reasons, for eg: the app is huge, with lot of know bugs and I don't want to make 1 and test it for 1 week, other is the deadline.

Please don't suggest it shouldn't be like this and must rewritten. I know.

How to deal with this controller to force Portait ?

Please read the bolded text too: can't force the whole app to support only Portait for 1 view controller, there are many on stack!

  • all view controllers on your stack must return `YES` in response to `shouldAutorotateToInterfaceOrientation:` for it to work. – nielsbot Sep 05 '12 at 18:09
  • they are returning YES. and they are rotating, which is good, only the last element I want to be fixed. –  Sep 05 '12 at 18:14
  • yes, sorry. see eric's answer then – nielsbot Sep 05 '12 at 18:17
  • it is not good, beside it is suggesting to change at app level, which I really don't want. –  Sep 05 '12 at 18:19
  • have you registered your non-rotateable view controller as a child of the root view controller that is rotating? if you answer "NO" the root view controller shoudl also answer "NO" – nielsbot Sep 05 '12 at 18:31
  • the parent/child it is from ios5 acording to http://www.iphonedevsdk.com/forum/iphone-sdk-development-advanced-discussion/95758-child-view-controllers-are-cool.html I have maked my question as ios4, not ios5, but thanks for try. –  Sep 05 '12 at 19:29

2 Answers2

2

Try marking the app's supported Interface orientations in the properties file to only being portrait. But then of course in that function you just return YES on view controllers that you want to allow rotation. But then when you push it back in the stack the other views should be portrait.

portrait only orientations

Eric Welander
  • 560
  • 4
  • 11
  • I told it must support all orientation: " The root controller must return Yes for all cases, because there are a few other UIViewControllers on the stack, which has / must have Portait / Landscape support." And is useless to tell me to change to support only portait the whole app –  Sep 05 '12 at 18:13
  • Just have this in your root view controller and each other VC that you want to rotate: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } – Eric Welander Sep 05 '12 at 18:15
  • Tryed: wasted the time, it worth DOWNVOTES. –  Sep 05 '12 at 18:18
  • it is funny yo see it is not working and worth downvotes and he still got upvotes :) –  Sep 06 '12 at 11:10
  • 1
    I was trying to help you. Not ever solution suggested is going to work perfectly. It's all about trial and error. I'm sorry my solution "didn't work" for you, but it might be of use to others. It at least helps others trying to answer your question to know my option doesn't work for your specific version of the problem. – Eric Welander Sep 06 '12 at 17:58
1

detect the Landscape rotation and rotate to Portait:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{ 

    UIInterfaceOrientation appOrientation = [UIApplication sharedApplication].statusBarOrientation;
    float width = self.view.bounds.size.width;
    float height = self.view.bounds.size.height;
    //NSLog(@"width %3.0f, height: %3.0f", width, height);

    if((fromInterfaceOrientation == UIInterfaceOrientationPortrait || fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)){
        // if is rotated from Portait:
        if((appOrientation == UIInterfaceOrientationLandscapeLeft || appOrientation == UIInterfaceOrientationLandscapeRight)){
            // to Landscape:
            CGAffineTransform transform = self.view.transform;
            transform = CGAffineTransformRotate(transform, -(M_PI / 2.0));
            self.view.transform = transform;            

            [self.view setBounds:CGRectMake(0, 0, height, width)];
        }
    }
    else {
        // it is rotated from Landscape:
        if((appOrientation == UIInterfaceOrientationPortrait || appOrientation == UIInterfaceOrientationPortraitUpsideDown)){
            // to Portrait:            
            CGAffineTransform transform = self.view.transform;
            transform = CGAffineTransformRotate(transform, +(M_PI / 2.0));
            self.view.transform = transform;            

            [self.view setBounds:CGRectMake(0, 0, height, width)];
        }
    } 
}

it isn't the best programming paradigm, but it does the trick.

Somebody write similar like tis to accept his answer, or write a better method, if you can!