是否可以在UITabBarController中显示SFSafariViewController? [英] Is it possible to display a SFSafariViewController inside of a UITabBarController?

查看:112
本文介绍了是否可以在UITabBarController中显示SFSafariViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在选项卡内加载 SFSafariViewController ,因此标签栏位于整个Safari视图的底部。

I want to load SFSafariViewController inside of a tab, so the tab bar is at the bottom of the entire Safari view.

这可能吗?我试了这个没有运气:

Is this possible? I tried this with no luck:

[self.tabBarController presentViewController:sfController animated:YES completion:nil];

必需 Safari视图是否为全屏?

Is it required that the Safari view be full screen?

推荐答案

使用JAL的答案作为基础,我能够在一个已经具有选项卡的现有结构的应用程序中实现这一点。

Using JAL's answer as a base, I was able to implement this myself in an app that had existing structure with tabs already.

在现有视图上按下按钮后,我希望标签#3进入选项卡中的Safari控制器,而不是将Safari控制器弹出到自己的窗口中它使用Apple的默认代码。

I wanted tab #3 to go into a Safari controller within the tab after a button was pressed on the existing view, and not pop the Safari controller into its own window like it does using Apple's default code.

关键是将 SFSafariViewController 交换到现有的<$ c $中c> UITabBarController 的视图控制器数组。当我在Safari控制器上按下完成按钮时,我将现有的原始视图控制器保存在选项卡#3(索引2)上以返回到它。

The key was to swap in a SFSafariViewController into the existing UITabBarController's array of view controllers. I saved the existing original view controller on tab #3 (index 2) to come back to it when the Done button was pressed on the Safari controller.

以下是我按下按钮时使用我的标签进入Safari控制器的操作:

Here's what I did to go into the Safari controller from with my tab when a button was pressed:

NSMutableArray *viewArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
self.savedController = [viewArray objectAtIndex:2];
[viewArray replaceObjectAtIndex:2 withObject:safariController];
self.tabBarController.viewControllers = viewArray;
[self setTabIconTitle];

然后,当按下完成按钮时,我可以像这样交换回该标签上的原始视图使用此委托调用的Safari控制器:

Then I could swap back to the original view on that tab like this when the Done button was pressed on the Safari controller using this delegate call:

- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller
{
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    NSMutableArray *viewArray = [NSMutableArray arrayWithArray:tabBarController.viewControllers];
    [viewArray replaceObjectAtIndex:2 withObject:self.savedController];
    tabBarController.viewControllers = viewArray;
    [self setTabIconTitle];
}

当我在 tabBarController中交换控制器时查看控制器数组,我丢失了标签图标和标签名称,所以我不得不设置它们。以下是我修复该问题的方法(并在触摸标签图标时保持主题):

When I swapped controllers in an out of the tabBarController view controller array, I lost my tab icon and tab name, so I had to set those. Here is how I fixed that issue (and kept my theming when the tab icon was touched):

- (void)setTabIconTitle
{
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UITabBar *tabBar = tabBarController.tabBar;
    UITabBarItem *marketplaceTab = [tabBar.items objectAtIndex:2];
    marketplaceTab.image = [[UIImage imageNamed:@"tab2-icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    marketplaceTab.selectedImage = [UIImage imageNamed:@"tab2-icon"];
    marketplaceTab.title = @"My Tab";
}

我必须承认我不确定Apple是否打算 SFSafariViewController 在选项卡中以这种方式使用,基于调用 SFSafariViewController 当前的正常行为。请注意,未来的iOS更新可能会改变此行为,并在新的iOS版本进入Beta版时始终测试您的代码。

I must admit that I am not sure that Apple intended that the SFSafariViewController be used in this way within a tab, based on what the normal behavior of calling SFSafariViewController currently does. Just be aware that future iOS updates may change this behavior and always test your code when new iOS versions go into Beta.

这篇关于是否可以在UITabBarController中显示SFSafariViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆