如何在单个视图中最好地处理纵向模式和横向模式?

时间:2011-04-25 09:48:59

标签: ios4 screen-orientation uiinterfaceorientation

我目前的理解是我必须在代码本身中分配坐标,而不是使用interface-builder。有更好的方法吗?

目前我正在尝试处理登录视图。它在纵向模式下工作良好,但在横向模式下,很少有内容被隐藏。请建议如何处理。

由于

GuruPrasad R Gujjar。

1 个答案:

答案 0 :(得分:0)

您可以采用以下两种方式之一。第一个是你建议并改变它们的所有帧,第二个是只有一个处于横向模式的单独视图并在旋转时将其切换出来。

对于这两种情况,您需要实现这两种方法

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    if (toInterfaceOrientation==UIInterfaceOrientationPortrait || toInterfaceOrientation== UIInterfaceOrientationPortraitUpsideDown) {
        //put the portrait orientation here
        self.usernameField.frame=CGRectMake(xPortraitLocation, yPortraitLocation, xWidth, yWidth);
        ...

    }
    else{
        //do the same thing with everything but for landscape mode
    }

使用单独的视图,您可以通过说self.view = self.lanscapeView或self.portraitView来切换它们。

如果我要使用第一种方法,我有时会在界面构建器中创建一个假视图,这样我就可以将所有项目放在正确的位置,并查看位置选项卡,这样可以消除猜测并检查分配数字。

希望这有帮助。