1

I have a tab bar application where everything is working fine. I have rotations of the device all working fine with the various Tab Bar View controllers.

Alas it was suggested that a couple of the View Controllers needed a help page. To this end I created a new ViewController that contains a UIWebView (where help can be built into an HTML file).

I create the new "HelpViewController" as follows:

mpHelpPage     = [[HelpPageViewController alloc] init];
[mTabBarController.view addSubview: mpHelpPage.view];
[mpHelpPage retain];

mpHelpPage.view.alpha = 0.75f;

This brings up the help page no problems when I'm in portait mode. Unfortunately when I'm in landscape mode and I do the above code it adds the HelpViewController in Portrait (meaning it extends off the bottom of the screen).

As such when I alloc the ViewController above I need some way of telling the ViewController to rotate to the current device orientation.

I am, however, at a loss as to how to get it to do this. Any help would be much appreciated!

Goz
  • 61,365
  • 24
  • 124
  • 204

3 Answers3

3

I handle this annoyance by putting an orientation check in viewWillAppear:, e.g.

if (self.interfaceOrientation == UIInterfaceOrientationPortrait || 
    self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    // custom code or call willRotate
} else {
    // custom code or call willRotate
}    

You can also do this if you prefer

if (UIDeviceOrientationIsPortrait(self.interfaceOrientation)) {
    // custom code or call willRotate
} else {
    // custom code or call willRotate
}    
Goz
  • 61,365
  • 24
  • 124
  • 204
PengOne
  • 48,188
  • 17
  • 130
  • 149
  • How would you go about doing an orientation check and do I have to manually move everything around? Can I just re-size the view controller's super view and expect all the sub views to resize as well? – Goz Sep 13 '11 at 16:11
  • You can resize the superview so long as every subview has an autoresizing mask. I prefer to call my `willRotateToInterfaceOrientation:duration:` function to avoid redundant code. – PengOne Sep 13 '11 at 16:14
  • Understood. But how do i know the devices orientation? – Goz Sep 13 '11 at 16:23
  • `[[UIDevice currentDevice] orientation];` – PengOne Sep 13 '11 at 17:34
  • Should I just be able to call "[self willRotateToInterfaceOrientation: [[UIDevice currentDevice] orientation] duration:1.0f];"? because that doesn't seem to work :( – Goz Sep 13 '11 at 18:25
  • @Goz: Apparently not. I put my animated rotation code into a separate method that I call from `willRotate...` and I also call that method from `viewWillAppear:`. – PengOne Sep 13 '11 at 18:53
  • So, in summary, there isn't a simple "rotateTheDamnedThingNow" call? ;) – Goz Sep 13 '11 at 19:09
  • Ok I've used something similar I check the device orientation and set the frame by swapping the width/height around on the view's frame before calling setNeedsLayout. – Goz Sep 13 '11 at 19:29
2

you should either set the frame-property of your subview in willRotateToInterfaceOrientation:duration: of your ViewController

or you write your own View and set the frame-property in layoutSubviews of your View

The added Subview should handle the layout of its subviews.

thomas
  • 5,637
  • 2
  • 24
  • 35
1

Since you've added HelpViewController as a subview and no UIViewController controls it, it will not be resized. You can resize HelpViewController's view manually by detecting a change in the orientation in the shouldAutorotateToInterfaceOrientation: method of the current UIViewController. This method passes the current orientation as its argument, so just check which is the current orientation and set a frame accordingly as:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if((interfaceOrientation==UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation==UIInterfaceOrientationLandscapeRight))
        mpHelpPage.view.frame = CGRectMake(0,0,480,300);
    else
        mpHelpPage.view.frame = CGRectMake(0,0,320,460);
    return YES;
}

Or, Instead of adding HelpViewControlleras a subView, try [self.navigationController pushViewController:HelpViewController animated:YES];

tipycalFlow
  • 7,594
  • 4
  • 34
  • 45
  • Then you should push it onto the stack of view controllers. Right now, you've still only added it as a subview ! – tipycalFlow Sep 13 '11 at 17:00
  • But I'm not using a navigation controller ... I'm using a tab bar controller ... If i use PushViewController onto the tab bar controller's navigation controller than, unsurprisingly, nothing happens. – Goz Sep 13 '11 at 18:24
  • Agreed, this would work only if you already had a navigation controller for a [particular tab](http://stackoverflow.com/questions/369128/tab-bar-application-with-navigation-controller)...that's why you have to manually resize it in your case. – tipycalFlow Sep 14 '11 at 04:08