旋转时自动调整动态视图大小

时间:2011-09-13 16:02:37

标签: iphone objective-c uiviewcontroller autorotate

我有一个标签栏应用程序,一切正常。我使用各种Tab Bar View控制器完全正常工作。

唉,有人建议一些视图控制器需要一个帮助页面。为此,我创建了一个包含UIWebView的新ViewController(可以将帮助内置到HTML文件中)。

我按如下方式创建新的“HelpViewController”:

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

mpHelpPage.view.alpha = 0.75f;

当我处于肖像模式时,这会显示帮助页面没有问题。不幸的是,当我处于横向模式并且我执行上面的代码时,它在Portrait中添加了HelpViewController(意味着它延伸到屏幕底部)。

因此,当我在上面分配ViewController时,我需要一些方法来告诉ViewController旋转到当前的设备方向。

但是,我对如何做到这一点感到茫然。任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:3)

我通过在viewWillAppear:中进行方向检查来处理这种烦恼,例如

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

如果您愿意,也可以这样做

if (UIDeviceOrientationIsPortrait(self.interfaceOrientation)) {
    // custom code or call willRotate
} else {
    // custom code or call willRotate
}    

答案 1 :(得分:2)

您应该在ViewController的frame中设置子视图的willRotateToInterfaceOrientation:duration: - 属性

或者您编写自己的视图并在视图的layoutSubviews中设置框架属性

添加的Subview应该处理其子视图的布局。

答案 2 :(得分:1)

由于您已将HelpViewController添加为子视图且没有UIViewController控制它,因此不会调整其大小。您可以通过检测当前HelpViewController的{​​{1}}方法中方向的更改来手动调整shouldAutorotateToInterfaceOrientation:视图的大小。此方法将当前方向作为其参数传递,因此只需检查当前方向,并相应地将框架设置为:

UIViewController

或者,不要将- (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; } 添加为子视图,而是尝试HelpViewController