简体   繁体   English

是否可以在UITabBarController内部显示SFSafariViewController?

[英]Is it possible to display a SFSafariViewController inside of a UITabBarController?

I want to load SFSafariViewController inside of a tab, so the tab bar is at the bottom of the entire Safari view. 我想将SFSafariViewController加载到选项卡内部,因此选项卡栏位于整个Safari视图的底部。

Is this possible? 这可能吗? I tried this with no luck: 我没有运气尝试过这个:

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

Is it required that the Safari view be full screen? 是否需要将Safari的观点是全屏幕?

I was able to achieve this programmatically. 我能够以编程方式实现这一目标。 They key to not have the UITabBar overlay on top of your UIViewController is to set translucent to NO : 他们的关键是要在UIViewController顶部UITabBar覆盖UITabBar ,而是将translucent设置为NO

In your AppDelegate.m: 在您的AppDelegate.m中:

@import SafariServices;

// ...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    tabBarController.tabBar.translucent = NO;

    SFSafariViewController *firstVC = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:@"http://stackoverflow.com"]];

    firstVC.title = @"SFSafariViewController";

    UIViewController *secondVC = [[UIViewController alloc] init];
    secondVC.view.backgroundColor = [UIColor blueColor];

    secondVC.title = @"Blue VC";

    tabBarController.viewControllers = @[firstVC, secondVC];

    self.window.rootViewController = tabBarController;
    [self.window makeKeyAndVisible];

    return YES;
}

Using JAL's answer as a base, I was able to implement this myself in an app that had existing structure with tabs already. 使用JAL的答案作为基础,我可以在具有结构和选项卡的现有应用程序中自己实现此功能。

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. 我希望选项卡#3在现有视图上按下按钮后进入选项卡内的Safari控制器,而不是像使用Apple的默认代码一样将Safari控制器弹出到其自己的窗口中。

The key was to swap in a SFSafariViewController into the existing UITabBarController 's array of view controllers. 关键是将SFSafariViewController交换到现有UITabBarController的视图控制器数组中。 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. 我将现有的原始视图控制器保存在选项卡#3(索引2)中,以便在Safari控制器上按下“完成”按钮时返回到它。

Here's what I did to go into the Safari controller from with my tab when a button was pressed: 这是在按下按钮时从选项卡进入Safari控制器的操作:

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

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: 然后,当使用此委托调用在Safari控制器上按下“完成”按钮时,可以像这样交换回到该选项卡上的原始视图:

- (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];
}

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. 当我在tabBarController视图控制器数组的外部交换控制器时,我丢失了标签图标和标签名称,因此必须进行设置。 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";
}

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. 我必须承认,我不知道,苹果意欲SFSafariViewController这样一个标签内使用,基于什么叫正常的行为SFSafariViewController目前做。 Just be aware that future iOS updates may change this behavior and always test your code when new iOS versions go into Beta. 请注意,将来的iOS更新可能会更改此行为,并在新的iOS版本进入Beta版时始终测试您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM