ViewController swift CustomNavBarWebView Created by entropy on 05 10 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// ViewController.swift
// CustomNavBarWebView
//
// Created by entropy on 05/10/2019.
// Copyright © 2019 kovalenko. All rights reserved.
//
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet private weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
setupWebView()
let backButton = UIBarButtonItem(title: "back", style: .plain, target: self, action: #selector(backButtonTouchedUpInside))
let myNav = UINavigationItem()
myNav.leftBarButtonItem = backButton
let navBar = UINavigationBar()
navBar.isTranslucent = false
navBar.delegate = self
navBar.backgroundColor = .white
navBar.items = [myNav]
view.addSubview(navBar)
navBar.translatesAutoresizingMaskIntoConstraints = false
navBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
navBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
if #available(iOS 11, *) {
navBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
} else {
navBar.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
}
}
private func setupWebView() {
guard let url = URL(string: "https://www.yandex.ru") else { return }
webView.load(URLRequest(url: url))
}
@objc private func backButtonTouchedUpInside() {
print("BAAACK")
}
}
extension ViewController: UINavigationBarDelegate {
public func position(for bar: UIBarPositioning) -> UIBarPosition {
return .topAttached
}
}