【问题标题】:Forcing UIActionSheet to use a specific orientation强制 UIActionSheet 使用特定方向
【发布时间】:2011-11-11 21:40:09
【问题描述】:

我的应用有一个无法改变方向的视图控制器。把它想象成一个需要固定在原地的运动场。

我现在想放置一个 UIActionSheet,但考虑到用户拿着设备的方式,我想将工作表与方向匹配。

似乎 UIActionSheet 与 MFComposerViewController 不同,它没有从我根据方向旋转的 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 成为它的子级。但是,这意味着我的比赛场地被新的视图控制器完全遮蔽了。)

【问题讨论】:

    标签: ios uiviewcontroller uiactionsheet uiinterfaceorientation


    【解决方案1】:

    原来答案很简单。添加此代码以强制 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 6 打破了这一点。我正在寻找解决方法。
    • iOS 6 破坏了很多东西,例如自动旋转,特别是如果您为 4.x 编译并在 6.x 设备上运行它。某些应用程序根本无法按预期自动旋转。可能你的代码破坏问题和这个自动旋转的东西有一些关系。
    • 好吧,iOS6终于删除了- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)
    • 问题不在于。如果您将应用程序编译为与 iOS 4.x 兼容,则它必须在 iOS 6 上正确运行。iOS 6 不能期望 -(void)shoultAutorotate 在 4.3 应用程序上正常工作。这显然是一个错误或错误的设计决策。
    【解决方案2】:

    勾选这个替代方案 -> JonasGessner/JGActionSheet 如果你不介意基本布局,或者你可以在 src 代码中修改它。

    这在 iOS 8.0.2 上对我有用。如下所示:

    1. 将其包含在您的项目中。

    JGActionSheet.hJGActionSheet.m 复制到您的项目中

    1. 将其导入您要使用操作表的位置。

    #import "JGActionSheet.h"

    1. 使用它。

    示例代码:

    JGActionSheetSection *menuSection = [JGActionSheetSection sectionWithTitle:nil message:@"Select Food" buttonTitles:@[@"Hamburger", @"Toast", @"Rice"] buttonStyle:JGActionSheetButtonStyleDefault];
    [menuSection setButtonStyle:JGActionSheetButtonStyleRed forButtonAtIndex:0];
    
    JGActionSheetSection *cancelSection = [JGActionSheetSection sectionWithTitle:nil message:nil buttonTitles:@[@"Cancel"] buttonStyle:JGActionSheetButtonStyleCancel];
    
    NSArray *sections = @[menuSection, cancelSection];
    JGActionSheet *sheet = [JGActionSheet actionSheetWithSections:sections];
    
    [sheet setButtonPressedBlock:^(JGActionSheet *sheet, NSIndexPath *indexPath) {
        [sheet dismissAnimated:YES];
    }];
    
    // Set specific orientation to action sheet
    sheet.transform = CGAffineTransformMakeRotation(M_PI);
    
    [sheet showInView:self.view animated:YES];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2012-02-24
      • 2018-05-25
      相关资源
      最近更新 更多