使用Swift 4关闭WKWebView并更改ViewController

时间:2019-02-22 16:50:37

标签: ios swift wkwebview

我要熟悉Swift 4和Xcode。

打开WKWebView后,我不知道如何关闭它!

我正在使用它来显示PDF文档,并添加了带有“完成”按钮的导航栏。

我已经找到了如何关闭它并转到Root UIViewController的方法,但是我希望它进入名为'DocumentsViewController'的视图控制器。

下面是我正在使用的代码,但是我相信我要修复的行是: let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: nil, action: #selector(selectAction(_:)))

selectAction为:

@objc func selectAction(_ sender:UIBarButtonItem) -> Void {
    //print("Select Clicked")
    self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
}

我花了数小时试图弄清楚!当我单击完成按钮时,如何关闭WKWebView并转到“ DocumentsViewController”?

@objc func selectAction(_ sender:UIBarButtonItem) -> Void {
    //print("Select Clicked")
    self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()

    if let url = Bundle.main.url(forResource: pdfTitle, withExtension: "pdf") {
    let webView = WKWebView(frame: self.view.frame)
    let urlRequest = URLRequest(url: url)
    webView.load(urlRequest as URLRequest)
    self.view.addSubview(webView)
    self.tabBarController?.tabBar.isHidden = true

    let screenSize: CGRect = UIScreen.main.bounds

    let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: 44))
    self.view.addSubview(navBar);

    let navItem = UINavigationItem(title: "");
    let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: nil, action: #selector(selectAction(_:)))

    navItem.rightBarButtonItem = doneItem;

    navBar.setItems([navItem], animated: false);

    // Get height of status bar (iPhone X introduced a new status bar)
    let statusBarHeight = UIApplication.shared.statusBarFrame.height
    // Initialize the frame
    webView.frame = CGRect.init(x: 0, y: statusBarHeight, width: view.bounds.maxX, height: view.bounds.maxY)
    // Set background color of status bar (optional)
    self.view.backgroundColor = UIColor(red: 248/255.0, green: 248/255.0, blue: 248/255.0, alpha: 1.0)
}

1 个答案:

答案 0 :(得分:0)

只需将新视图添加到导航堆栈...

@objc func selectAction(_ sender:UIBarButtonItem) -> Void {
let newViewController = DocumentsViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
}