1

我已经成功地以纵向模式查看应用程序。问题是,当我在风景中查看它时,它似乎不合适。我已经实现了这段代码。

if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft)
{
    return interfaceOrientation=UIInterfaceOrientationLandscapeLeft;
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
    return interfaceOrientation=UIInterfaceOrientationLandscapeRight;
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait)
{
   return interfaceOrientation=UIInterfaceOrientationPortrait;
}   

我应该做些什么额外的事情吗?这段代码对我有帮助吗?

4

1 回答 1

1

shouldAutorotateToInterfaceOrientation:如果您想支持所有可能的方向,请将您的实现更改为以下内容:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

如果您只想支持 :和UIInterfaceOrientationLandscapeLeft,则将代码更改为:UIInterfaceOrientationLandscapeRightUIInterfaceOrientationPortrait

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}

您不需要寻找新的方向,[UIApplication sharedApplication].statusBarOrientation因为您已经将它作为参数。


如果要在设备旋转时重新定位视图的元素,则必须使用它们的autoresizingMask属性。另一种选择是实现该方法willRotateToInterfaceOrientation

您可以在Size InspectorautoresizingMask的 Interface Builder 中更改视图的元素。尝试尝试不同的组合,直到获得所需的结果。

在此处输入图像描述

最后,请注意,如果您的视图很复杂或在每个方向上完全不同,最好的选择是使用两个 xib 文件:一个用于纵向,一个用于横向。

于 2012-03-12T13:11:45.117 回答