簡體   English   中英

是否可以在UITabBarController內部顯示SFSafariViewController?

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

我想將SFSafariViewController加載到選項卡內部,因此選項卡欄位於整個Safari視圖的底部。

這可能嗎? 我沒有運氣嘗試過這個:

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

是否需要將Safari的觀點是全屏幕?

我能夠以編程方式實現這一目標。 他們的關鍵是要在UIViewController頂部UITabBar覆蓋UITabBar ,而是將translucent設置為NO

在您的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;
}

使用JAL的答案作為基礎,我可以在具有結構和選項卡的現有應用程序中自己實現此功能。

我希望選項卡#3在現有視圖上按下按鈕后進入選項卡內的Safari控制器,而不是像使用Apple的默認代碼一樣將Safari控制器彈出到其自己的窗口中。

關鍵是將SFSafariViewController交換到現有UITabBarController的視圖控制器數組中。 我將現有的原始視圖控制器保存在選項卡#3(索引2)中,以便在Safari控制器上按下“完成”按鈕時返回到它。

這是在按下按鈕時從選項卡進入Safari控制器的操作:

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

然后,當使用此委托調用在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];
}

當我在tabBarController視圖控制器數組的外部交換控制器時,我丟失了標簽圖標和標簽名稱,因此必須進行設置。 這是我解決該問題的方法(並在觸摸選項卡圖標時保持主題不變):

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

我必須承認,我不知道,蘋果意欲SFSafariViewController這樣一個標簽內使用,基於什么叫正常的行為SFSafariViewController目前做。 請注意,將來的iOS更新可能會更改此行為,並在新的iOS版本進入Beta版時始終測試您的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM