从UITabBarController调用SFSafariViewController并在按下“完成”后返回

时间:2019-06-10 12:31:37

标签: ios swift uitabbarcontroller sfsafariviewcontroller sfsafariviewcontrollerdelegate

我有一个带有自定义按钮的自定义UITabBarController。当我单击此按钮时,我打开SFSafariViewController-此处一切正常。但是,当我单击SFSafariViewController中的“完成” 按钮时,该按钮将关闭。但是我无法返回到UITabBarController,只能看到我在应用程序委托窗口中添加的背景颜色。

示例代码:

class TabBar: UITabBarController, UITabBarControllerDelegate, SFSafariViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
    }

    func setupCenterButton() {
        let centerButton = CenterButton(frame: CGRect(x: 0, y: 0, width: 74, height: 74))
        centerButton.layer.cornerRadius = 37
        centerButton.frame.origin.y = self.tabBar.frame.minY - 37
        centerButton.frame.origin.x = view.bounds.width/2 - 37
        centerButton.setImage(UIImage(named: "google"), for: .normal)
        centerButton.addTarget(self, action: #selector(openURL), for: .touchUpInside)
        view.addSubview(centerButton)
        view.layoutIfNeeded()
    }

    @objc func openURL() {
        let termsURL = SFSafariViewController(url: URL(string: "https://google.ru")!)
        termsURL.modalPresentationStyle = .currentContext
        termsURL.delegate = self
        self.present(termsURL, animated: true, completion: nil)
    }
}

如何在UITabBarController之后显示SFSafariViewController

2 个答案:

答案 0 :(得分:0)

func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
    //To access the  Specific tabBar ViewController
    let tabBarController = storyboard?.instantiateViewController(withIdentifier: "IdentifierTabbar") as! TabBar
   // tabBarController.isComingFrom = "Settings" // Assign the Value                
    window?.rootViewController = tabBarController
}

答案 1 :(得分:0)

我遇到了同样的问题,当我在SafariViewController上点击“完成”时,以编程方式选择了另一个选项卡栏项目。在示例中,我选择了标签栏的第一项。

import UIKit
import SafariServices
    
class WebViewController: UIViewController, SFSafariViewControllerDelegate {
        
    override func loadView() {

    }
    override func viewDidLoad() {
        super.viewDidLoad()
        guard let url = URL(string: "<your website url>") else {
            return
        }

        let safariVC = SFSafariViewController(url: url)
        present(safariVC, animated: true, completion: {self.selectFirstTab()})
        
    }
    
    func selectFirstTab(){
        self.tabBarController?.selectedIndex = 0
    }

}