在 iOS 9 中,为什么 SFSafariViewController 被推送而不是模态显示?

In iOS 9, why is SFSafariViewController is being pushed instead of presented modally?

我通过在 UIViewController 实例上调用 presentViewController:animated:completion: 来展示 SFSafariViewController

结果是 pushed on(从右边滑入),就像我在 UINavigationController 实例上调用 pushViewController:animated: 一样。我已经证实这一切都发生在主队列上。并且呈现视图控制器本身不是模态(这应该无关紧要,但为了以防万一,我们可以排除这种可能性)。

如果我用 UIViewController 替换 SFSafariViewController,它会按预期工作,以模态方式呈现。

weakSelf.oAuthViewController = [[SFSafariViewController alloc] initWithURL:url];
[viewController presentViewController:weakSelf.oAuthViewController animated:YES completion:nil];

知道为什么或如何解决这个问题吗?

我刚遇到同样的问题。此外,即使您没有设置委托,完成按钮也能正常工作。不知道为什么会这样。但是,我找到了一个解决方法:将 safari 控制器包装在导航控制器中并隐藏导航栏。

func openURL(url:NSURL) {

    if #available(iOS 9.0, *) {
        let safariController = SFSafariViewController(url: url)
        safariController.delegate = self
        let navigationController = UINavigationController(rootViewController: safariController)
        navigationController.setNavigationBarHidden(true, animated: false)
        self.present(navigationController, animated: true, completion: nil)
    } else {
        UIApplication.sharedApplication().openURL(url)
    }
}

Objective-C 版本的 iGerms 答案:

-(void)openURL:(NSURL *)url {
   SFSafariViewController *safariController = [[SFSafariViewController alloc]initWithURL:url];
   safariController.delegate = self;
   UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:safariController];
   [navigationController setNavigationBarHidden:YES animated:NO];
   [self presentViewController:navigationController animated:YES completion:nil];
}

这是获得 SFSafariViewController 的垂直模态显示的简单方法:

let safari = SFSafariViewController(URL: url)
safari.modalPresentationStyle = .overFullScreen
presentViewController(safari, animated: true, completion: nil)

要使用默认模式过渡样式,只需将过渡代理设置为 self 即可。

let svc = SFSafariViewController(url: url)
svc.transitioningDelegate = self //use default modal presentation instead of push
present(svc, animated: true, completion: nil)

您需要在视图控制器中采用 UIViewControllerTransitioningDelegate 协议,但没有实现所需的功能。

这在 Session 225 at WWDC, What's New in Safari View Controller 中提到过。