开发者

Auto-Resizing a dynamic view when rotated

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!


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
}    


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.


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];

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜