【问题标题】:Is it possible to display a SFSafariViewController inside of a UITabBarController?是否可以在 UITabBarController 内显示 SFSafariViewController?
【发布时间】:2016-05-07 03:50:48
【问题描述】:

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

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

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

Safari 视图是否需要全屏?

【问题讨论】:

    标签: ios objective-c uitabbarcontroller sfsafariviewcontroller


    【解决方案1】:

    我能够以编程方式实现这一目标。他们在UIViewController 上没有覆盖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;
    }
    

    【讨论】:

      【解决方案2】:

      使用 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 的正常行为,我不确定 Apple 是否打算在选项卡中以这种方式使用 SFSafariViewController。请注意,未来的 iOS 更新可能会改变这种行为,并始终在新的 iOS 版本进入 Beta 版时测试您的代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-19
        相关资源
        最近更新 更多