草庐IT

iOS 8 UIActionSheet 忽略 View Controller supportedInterfaceOrientations 和 shouldAutorotate

coder 2023-07-25 原文

我有一个应用程序,它通过 plist 文件配置为支持纵向、左侧横向和右侧横向(即 UISupportedInterfaceOrientations 设置为 UIInterfaceOrientationPortrait、UIInterfaceOrientationLandscapeLeft 和 UIInterfaceOrientationLandscapeRight)。但我已将支持的方向限制为仅在 View Controller 内纵向。如果我在 View Controller 的 View 上显示 UIActionSheet 然后旋转设备,则 UIActionSheet 会旋转到新的方向。这仅发生在运行 iOS 8 (GM Seed) 的设备上。

我希望 UIActionSheet 遵循包含 View Controller 的规则而不是旋转。想法?

我不希望这种情况发生:

UIViewController 示例代码:

- (IBAction)onTouch:(id)sender
{
    UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:@"hi"
                                                       delegate:nil
                                              cancelButtonTitle:@"Cancel"
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:@"Open", nil];

    [actionSheet showInView:self.view];
}

#pragma mark - Rotation methods

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

最佳答案

这是我的解决方法,基于 Busrod 的解决方案并进行了一些修改,因为我在使用他的解决方法时在 UIActionSheet 中遇到了一些问题:

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    UIViewController *presentedViewController = window.rootViewController.presentedViewController;
    if (presentedViewController) {
        if ([presentedViewController isKindOfClass:[UIActivityViewController class]] || [presentedViewController isKindOfClass:[UIAlertController class]]) {
            return UIInterfaceOrientationMaskPortrait;
        }
    }
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

关于iOS 8 UIActionSheet 忽略 View Controller supportedInterfaceOrientations 和 shouldAutorotate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25819132/

有关iOS 8 UIActionSheet 忽略 View Controller supportedInterfaceOrientations 和 shouldAutorotate的更多相关文章

随机推荐