【问题标题】:Set Orientation to Portrait on iPhone在 iPhone 上将方向设置为纵向
【发布时间】:2010-07-22 12:58:20
【问题描述】:
我有一个包含登录名、3 个表和一个图像的应用程序。
您可以将图像旋转到横向模式,但我想要做的是当按下“返回”按钮并且应用程序返回上一个屏幕时,我是自动旋转以提供纵向视图的应用程序.
有什么办法吗?
【问题讨论】:
标签:
iphone
xcode
rotation
screen-orientation
autorotate
【解决方案1】:
对于以前的控制器:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
return YES;
}
return NO;}
【解决方案2】:
如果之前的视图控制器只支持纵向(参见shouldAutorotateToInterfaceOrientation:),那么它应该会自动旋转。
如果之前的视图控制器支持横向,但如果它最初是纵向的,您希望它旋转到纵向,您可以通过更改shouldAutorotateToInterfaceOrientation: 在导航返回时返回的内容来强制它。我不推荐这个;它是不一致的 UI(而且要弄清楚正在发生什么导航有点乏味)。
有多种方法可以设置视图控制器界面方向(-[UIDevice setOrientation:] 将尝试触发自动旋转),但随后您将进入私有 API 和潜在拒绝领域。
【解决方案3】:
在您的表格视图控制器中覆盖 shouldAutorotateToInterfaceOrientation: 并仅返回您想要的方向。
这实际上可能不会切换回方向,因此如果仅此一项不起作用,您可能必须在表格视图控制器的 viewWillAppear: 或 viewDidAppear: 方法中在 UIApplication 上调用 setStatusBarOrientation:animated:。