强制 UIActionSheet 使用特定方向
我的应用程序有一个无法更改方向的视图控制器。将其视为需要固定在适当位置的运动场。
我现在想要放置一个 UIActionSheet,但考虑到用户持有设备的方式,我希望将工作表与方向相匹配。
看来 UIActionSheet 与 MFComposerViewController 不同,它不是从 StatusBar 获取其方向(我根据方向旋转 StatusBar),而是从底层视图控制器获取其方向。换句话说,无论如何握住设备,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;
并且我设法获得正确的方向,但是生成的操作表从纵向一侧出现,并且其大小就好像它仍然处于纵向方向一样。操作表的框架是根据按钮的数量等计算的。
是否有人设法强制 UIActionSheet 以指定方向正确显示?
(我还尝试创建一个新视图,该视图已转换并具有正确的大小,但 UIActionSheet 忽略了它。剩下的就是创建一个自动旋转的新 UIViewController,并使 UIActionSheet 成为它的子级。但是,这意味着我的运动场被新的视图控制器完全遮挡了。)
My app has one view controller that cannot change orientation. Think of it as a playing field that needs to stay fixed in place.
I now want to put up a UIActionSheet, but given the way the user is holding the device, I'd like to match the sheet to the orientation.
It seems that the UIActionSheet, unlike, say, the MFComposerViewController, doesn't get its orientation from the StatusBar, which I am rotating based on orientation, but gets its orientation from the underlying view controller. In other words, the UIActionSheet comes up in portrait orientation, regardless of how the device is being held.
I have tried:
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;
And I manage to get the right orientation, but the resulting action sheet comes up from the portrait side, and is sized as if it were still in portrait orientation. The action sheet's frame is calculated based on the number of buttons, etc.
Has anyone managed to force UIActionSheet to properly display in a specified orientation?
(I also tried creating a new view, which was transformed and had the correct size, but UIActionSheet ignored it. What remains left is to create a new UIViewController which auto rotates, and have UIActionSheet be a child of it. However, that means that my playing field gets completely obscured by the new view controller.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明答案非常简单。添加此代码以强制 UIActionSheets 为您想要的任何方向:
lastUIAngle 是 pi/2 的倍数,在本例中,我从上次更改方向时保存了该值。
Turns out that the answer was pretty simple. Add this code to force UIActionSheets to whatever orientation you'd like:
lastUIAngle is a multiple of pi/2, which, in this case, I had saved from the last time the orientation was changed.
检查这个替代方案 -> JonasGessner/JGActionSheet 如果你不介意基本布局,也可以在 src 代码中修改它。
这对我在 iOS 8.0.2 上有效。就像下面这样:
将
JGActionSheet.h
和JGActionSheet.m
复制到您的项目中,#import "JGActionSheet.h"
示例代码:
Check this alternative -> JonasGessner/JGActionSheet if you don't mind the basic layout, or you can modify it in the src code.
This worked for me on iOS 8.0.2. Just like below:
Copy
JGActionSheet.h
andJGActionSheet.m
to you project#import "JGActionSheet.h"
Sample Code: