Guest User

Untitled

a guest
May 16th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // MySimpleApp
  4. //
  5.  
  6. import UIKit
  7.  
  8. class ViewController: UIViewController {
  9.  
  10. @IBOutlet weak var showPopupBtn: UIButton!
  11.  
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14.  
  15. }
  16.  
  17. override func didReceiveMemoryWarning() {
  18. super.didReceiveMemoryWarning()
  19. // Dispose of any resources that can be recreated.
  20. }
  21.  
  22.  
  23. @IBAction func showPopupTapped(_ sender: Any) {
  24. let popup = WebViewAsPopup()
  25. popup.showWebViewPopup(on: self)
  26. }
  27.  
  28. }
  29.  
  30. //
  31. // WebViewAsPopup.swift
  32. // MySimpleApp
  33. //
  34.  
  35. import WebKit
  36.  
  37. class WebViewAsPopup: NSObject, UIWebViewDelegate {
  38.  
  39. func showWebViewPopup(on controller: UIViewController) {
  40. // Popup background
  41. let bg = UIView()
  42. bg.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
  43. bg.layer.backgroundColor = UIColor.black.withAlphaComponent(0.5).cgColor
  44.  
  45. // Webview sizing
  46. let width = UIScreen.main.bounds.width - 60
  47. let height = UIScreen.main.bounds.height - 200
  48. let x = UIScreen.main.bounds.width/2 - width/2
  49. let y = UIScreen.main.bounds.height/2 - height/2
  50.  
  51. // Webview stuff
  52. let webView = UIWebView()
  53. webView.frame = CGRect(x: x, y: y, width: width, height: height)
  54. let url = URL(string: "https://google.com")
  55. let request = URLRequest(url: url!)
  56. // webView.delegate = self << it crash here
  57. webView.loadRequest(request)
  58.  
  59. // Styling webview popup
  60. webView.layer.borderWidth = 1
  61. webView.layer.borderColor = UIColor.black.cgColor
  62. webView.layer.masksToBounds = true
  63. webView.layer.cornerRadius = 10
  64.  
  65. // Add bg then webview to main view controller
  66. controller.view.addSubview(bg)
  67. controller.view.addSubview(webView)
  68. }
  69.  
  70. func webViewDidStartLoad(_ webView: UIWebView)
  71. {
  72. print("#webViewDidStartLoad!") // << it doesn't fire :(
  73. }
  74. func webViewDidFinishLoad(_ webView: UIWebView)
  75. {
  76. print("#webViewDidFinishLoad!") // << it doesn't fire :(
  77. }
  78.  
  79. }
Add Comment
Please, Sign In to add comment