ios - 强制 UIActionSheet 使用特定方向

标签 ios uiviewcontroller uiactionsheet uiinterfaceorientation

我的应用有一个无法改变方向的 View Controller 。将其视为需要固定在原地的运动场。

我现在想放置一个 UIActionSheet,但考虑到用户握持设备的方式,我想将表单与方向相匹配。

UIActionSheet 似乎不像 MFComposerViewController,它不是从 StatusBar 获取方向,我根据方向旋转它,而是从底层 View Controller 获取方向。换句话说,无论设备的握持方式如何,UIActionSheet 都会以纵向显示。

我试过:

CGAffineTransform   t = CGAffineTransformMakeRotation(lastUIAngle);
[actionSheet setTransform:t];
if (UIInterfaceOrientationIsLandscape(lastOrientation))
    actionSheet.frame = CGRectMake(0, 0, 480, 320);
else 
    actionSheet.frame = CGRectMake(0, 0, 320, 480);
actionSheet.center = self.view.center;

我设法获得了正确的方向,但是生成的操作表从纵向出现,并且大小就像它仍然是纵向一样。 action sheet的frame是根据按钮的数量等计算出来的。

有没有人设法强制 UIActionSheet 以指定的方向正确显示?

(我还尝试创建一个新 View ,它经过转换并具有正确的大小,但 UIActionSheet 忽略了它。剩下的就是创建一个自动旋转的新 UIViewController,并让 UIActionSheet 成为它的子项。但是,这意味着我的运动场被新的 View Controller 完全遮盖了。)

最佳答案

原来答案很简单。添加此代码以强制 UIActionSheets 到您想要的任何方向:

tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
tempView.transform = CGAffineTransformMakeRotation(lastUIAngle);
tempView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view.window addSubview:tempView];

UIActionSheet *actionSheet = ...

[actionSheet showInView:tempView];

lastUIAngle 是 pi/2 的倍数,在本例中,我从上次更改方向时保存了它。

关于ios - 强制 UIActionSheet 使用特定方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8100323/

相关文章:

iphone - 显示一个 UIActionSheet

ios - UIActionSheet/UIAlertController 多行文本

ios - 如何将图像放在 UIActionSheet 上?

IOS是否可以在不强制转换的情况下获取NSFetchedResultsController字段的数据

sqlite - SQLITE 数据库是否会在应用程序更新时重置?

ios - 在已获得焦点的模态 VC 中显示 UISearchController

ios - UIView 侧边栏始终在顶部

ios - In-Memory Realm 是否有 Swift 中磁盘持久化 Realm 数据的副本?

ios - UISearchBar CGContext 错误

ios - 刷新 UIViewController