Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // MySimpleApp
- //
- import UIKit
- class ViewController: UIViewController {
- @IBOutlet weak var showPopupBtn: UIButton!
- override func viewDidLoad() {
- super.viewDidLoad()
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- @IBAction func showPopupTapped(_ sender: Any) {
- let popup = WebViewAsPopup()
- popup.showWebViewPopup(on: self)
- }
- }
- //
- // WebViewAsPopup.swift
- // MySimpleApp
- //
- import WebKit
- class WebViewAsPopup: NSObject, UIWebViewDelegate {
- func showWebViewPopup(on controller: UIViewController) {
- // Popup background
- let bg = UIView()
- bg.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
- bg.layer.backgroundColor = UIColor.black.withAlphaComponent(0.5).cgColor
- // Webview sizing
- let width = UIScreen.main.bounds.width - 60
- let height = UIScreen.main.bounds.height - 200
- let x = UIScreen.main.bounds.width/2 - width/2
- let y = UIScreen.main.bounds.height/2 - height/2
- // Webview stuff
- let webView = UIWebView()
- webView.frame = CGRect(x: x, y: y, width: width, height: height)
- let url = URL(string: "https://google.com")
- let request = URLRequest(url: url!)
- // webView.delegate = self << it crash here
- webView.loadRequest(request)
- // Styling webview popup
- webView.layer.borderWidth = 1
- webView.layer.borderColor = UIColor.black.cgColor
- webView.layer.masksToBounds = true
- webView.layer.cornerRadius = 10
- // Add bg then webview to main view controller
- controller.view.addSubview(bg)
- controller.view.addSubview(webView)
- }
- func webViewDidStartLoad(_ webView: UIWebView)
- {
- print("#webViewDidStartLoad!") // << it doesn't fire :(
- }
- func webViewDidFinishLoad(_ webView: UIWebView)
- {
- print("#webViewDidFinishLoad!") // << it doesn't fire :(
- }
- }
Add Comment
Please, Sign In to add comment